Saturday, December 29, 2012

Difference between equals method and "==" operator in Java - Interview Question

Both equals() and "==" operator in Java is used to compare objects to check equality but main difference between equals method and  == operator is that former is method and later is operator. Since Java doesn’t support operator overloading, == behaves identical for every object but equals() is method, which can be overridden in Java and logic to compare objects can be changed based upon business rules. Another notable difference between == and equals method is that former is used to compare both primitive and objects while later is only used for objects comparison. At the same time beginners struggle to find when to use equality operator (==) and when to use equals method for comparing Java objects. In this tutorial we will see how equals() method and == operator works in Java and what is difference between "==" and equals method in Java and finally when to use "==" and equals() to compare objects.

What is "==" equality operator in Java
Difference between == and equals method in Java"==" or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. In terms of comparing primitives like boolean, int, float "==" works fine but when it comes to compare objects it creates confusion with equals method in Java. "==" compare two objects based on memory reference. so "==" operator will return true only if two object reference it is comparing represent exactly same object otherwise "==" will return false. After introduction of Autoboxing and unboxing in Java 5, using == to compare wrapper objects even become trickier because some time they can return unexpected result. See my post what is problem with == operator in autoboxing world post Java 5 for more details.


What is equals method in Java
Equals() method is defined in Object class in Java and used for checking equality of two object defined by business logic e.g. two Employees are considered equal if they have same empId etc. You can have your domain object and than override equals method for defining condition on which two domain objects will be considered equal. equal has contract with hashcode method in Java and whenever you override equals method you also need to override hashcode() in Java. Default implementation of equals provided in Object class is similar to "==" equality operator and return true if you are comparing two references of same object. It’s one of the Java best practice to override equals in Java to define equality based on business requirement. It’s also worth noting that equals should be consistent with compareTo in Java, So that when you store objects in TreeMap or TreeSet Collection, which uses compareTo for checking equality, behavior remains consistent.

Difference between == and equals in Java

Main difference between == and equals in Java is that "==" is used to compare primitives while equals() method is recommended to check equality of objects. Another difference between them is that, If both "==" and equals() is used to compare objects than == returns true only if both references points to same object while equals() can return true or false based on its overridden implementation.One of the popular case is comparing two String in Java in which case == and equals() method return different results.

Comparing String with == and equals
String comparison is a common scenario of using both == and equals method. Since java.lang.String class override equals method, It return true if two String object contains same content but == will only return true if two references are pointing to same object. Here is an example of comparing two Strings in Java for equality using == and equals() method which will clear some doubts:

String personalLoan = new String("cheap personal loans");
String homeLoan = new String("cheap personal loans");
     
//since two strings are different object result should be false
boolean result = personalLoan == homeLoan;
System.out.println("Comparing two strings with == operator: " + result);
     
//since strings contains same content , equals() should return true
result = personalLoan.equals(homeLoan);
System.out.println("Comparing two Strings with same content using equals method: " + result);
     
homeLoan = personalLoan;
//since both homeLoan and personalLoand reference variable are pointing to same object
//"==" should return true
result = (personalLoan == homeLoan);
System.out.println("Comparing two reference pointing to same String with == operator: " + result);

Output:
Comparing two strings with == operator: false
Comparing two Strings with same content using equals method: true
Comparing two reference pointing to same String with == operator: true


Comparing two objects with "==" and equals.
Another scenario which creates confusion between == and equals method is when you compare two Objects. When you compare two references pointing to object of type Object you should see same result from both == operator and equals method because default implementation of equals method just compare memory address of two objects and return true if two reference variable are pointing towards exactly same object. Here is example of == vs equals method for comparing two objects:

Object obj1 = new Object();
Object obj2 = new Object();
     
// == should return false
result = (obj1==obj2);
System.out.println("Comparing two different Objects with == operator: " + result);
     
//equals should return false because obj1 and obj2 are different
result = obj1.equals(obj2);
System.out.println("Comparing two different Objects with equals() method: " + result);
     
// "==" should return true because both obj1 and obj2 points same object
obj1=obj2;
result = (obj1==obj2);
System.out.println("Comparing two reference pointing to same Object with == operator: " + result);

Output:
Comparing two different Objects with == operator: false
Comparing two different Objects with equals() method: false
Comparing two reference pointing to same Object with == operator: true


Summary
1) use == to compare primitive e.g. boolean, int, char etc, while use equals() to compare objects in Java.
2) == return true if two reference are of same object. Result of equals() method depends on overridden implementation.
3) For comparing String use equals() instead of  == equality operator.

That’s all on difference between equals method and  == operator in Java.  As I said main difference between them is that one of them is operator and other is method and == is used to compare both primitive and objects while equals() method is used to check equality of objects only.

Other Java Interview question articles from Javarevisited blog

No comments :

Post a Comment