forked from slgobinath/Java-Helps-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualityOperator.java
More file actions
41 lines (38 loc) · 1.02 KB
/
EqualityOperator.java
File metadata and controls
41 lines (38 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* The == operator is used to compare two primitive data types.
* If it is used with references, it checks whether two references are refering the same object.
*
* @author L.Gobinath
*/
public class EqualityOperator {
public static void main(String[] args) {
Student stu1 = new Student(100);
Student stu2 = new Student(100);
Student stu3 = stu1;
if (stu1 == stu2) { // false
System.out.println("stu1 == stu2");
}
if (stu1.equals(stu2)) { // true
System.out.println("stu1 equals stu2");
}
if (stu1 == stu3) { // true
System.out.println("stu1 == stu3");
}
}
}
class Student {
int index;
public Student(int index) {
this.index = index;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student stu = (Student) obj;
if (stu.index == this.index) {
return true;
}
}
return false;
}
}