Equality operator can be used to compare primitives but when you compare objects using ==, it just compares reference not the actual content of the objects.
Equals methods :
But in Equal method if you want to compare the actual content of the object, then we need to override equals method in our class. If we do not override object’s class equals method, then it behaves same as “==” and compares references only.
Difference between equals() and == in java
1) == is operator whereas equals is method in java.
2) == is recommended to compare primitives whereas equals method is recommended to compare the actual content of objects.
3) Equals method can be overridden but you can’t override behavior of “==” operator
4) == can be used with primitives and objects but you can’t use equals method with primitives.
Example
Equals and == in case of String
String is most common scenario where equals and == methods are use
StringEqualsAndOpetatorMain.java
public class StringEqualsAndOpetatorMain {
public static void main(String[] args) {
String name1=new String("John");
String name2=new String("John");
/*Even though content of objects is same, it will
* return false, as name1 and name2 points to different objects
*/
System.out.println(name1==name2);
/*Equals method compares content, so this will return true
*/
System.out.println(name1.equals(name2));
System.out.println("===================");
name2=name1;
/*Now name1 and name2 both point to same reference
* so this will return true
*/
System.out.println(name1==name2);
/*Equals method compares content, so this will return true
*/
System.out.println(name1.equals(name2));
}
}
When we Run the above program
false
true
===================
true
true
Equals and == in case of Custom objects
It is very much similar to String. Let’s create Employee class and override equals method. If two employees have same name and age then they will be considered as same.
Employee.java
public class Employee {
String name;
int age;
public Employee(String name, int age) {
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
Create another class for Employee comparison
EmployeeEqualsAndOpetatorMain.java
public class EmployeeEqualsAndOpetatorMain {
public static void main(String[] args) {
Employee emp1=new Employee("John",18);
Employee emp2=new Employee("John",18);
/*Even though content of objects is same, it will
* return false, as name1 and name2 points to different objects
*/
System.out.println(emp1==emp2);
/*Equals method compares content, so this will return true
*/
System.out.println(emp1.equals(emp2));
System.out.println("===================");
emp2=emp1;
/*Now name1 and name2 both point to same reference
* so this will return true
*/
System.out.println(emp1==emp2);
/*Equals method compares content, so this will return true
*/
System.out.println(emp1.equals(emp2));
}
}
Equality operator “==”:
Equality operator can be used to compare primitives but when you compare objects using ==, it just compares reference not the actual content of the objects.
Equals methods :
But in Equal method if you want to compare the actual content of the object, then we need to override equals method in our class. If we do not override object’s class equals method, then it behaves same as “==” and compares references only.
Difference between equals() and == in java
1) == is operator whereas equals is method in java.
2) == is recommended to compare primitives whereas equals method is recommended to compare the actual content of objects.
3) Equals method can be overridden but you can’t override behavior of “==” operator
4) == can be used with primitives and objects but you can’t use equals method with primitives.
Example
Equals and == in case of String
String is most common scenario where equals and == methods are use
When we Run the above program
Equals and == in case of Custom objects
It is very much similar to String. Let’s create Employee class and override equals method. If two employees have same name and age then they will be considered as same.
Create another class for Employee comparison
When we run above program, output will be: