About NewTechnoBuzz
Contact Us

Wednesday, July 16, 2014

Difference between "==" operator and equals() method

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 of Object class and later is an operator. What is difference between == and equals() in Java is one of the classical Interview Questions which is asked by many interviewers for 2-3 years experience Java developers.

This question is mostly asked in conjunction with String because comparing String using "==" and equals() method returns different results. Since Java doesn't support operator overloading, so "==" behaves identical for every object but equals() is method, which can be overridden in Java to compare objects based upon business rules.

Most of 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 them and finally when to use "==" and equals() to compare objects.

What is "==" equality operator in Java

"==" or equality operator in Java is a binary operator and is used to compare primitives and objects. In terms of comparing primitives (like boolean, int, float), "==" works fine but when it is used to compare objects then it creates confusion with equals method in Java. "==" compares two objects based on memory reference.
So, "==" operator will return true only if two object have same reference otherwise it will return false. After introduction of Autoboxing and unboxing in Java, comparison between wrapper objects using "==" operator becomes trickier because some time they can return unexpected result.

What is equals() method in Java

Equals() method is defined in Object class in Java and is used for checking equality of two object defined by business logic e.g. Two Employees are considered equal if they have same employeeId etc. Default implementation of equals() method 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() method to define equality based on business rules. 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, remains consistent.

For example java.lang.String class override the equals() and hashCode method and in overridden method it will check that two string contains same value or character if yes then they are equals other wise not equal.

Difference between == and equals() method

Now we know what is equals() method, how it works and what is equality operator (==) and how it compare objects. Now, its time to compare them. Here is some noting difference between equals() method and == operator in Java:

String comparison with == and equals()

String comparison is a common scenario of using both == and equals method. Since String class override equals() method, it returns true if two String objects contain same content, but "==" operator will only return true if two references are pointing to same object. Below are the examples of comparing Strings in Java for equality using "==" operator and equals() method:
String str1 = new String("Hello World");
String str2 = new String("Hello World");
boolean result = (str1 == str2);
System.out.println("String comparison with == operator: " + result);
result = str1.equals(str2);
System.out.println("String comparison using equals method: " + result);
str1 = str2;

result = (str1 == str2);
System.out.println("String comparison with == operator: " + result);

String comparison with == operator: false
String comparison using equals method: true
String comparison with == operator: true

Comparing two objects with "==" and equals.

Another scenario which creates confusion between "==" operator and equals() method is when you compare two objects. When you compare two references pointing to object of same class, you will see same result from both "==" operator and equals() method because default implementation of equals() method is to compare memory address of two objects. So, it returns true if two reference variables are pointing to same object. Below are the examples for comparing two objects:
Object object1 = new Object();
Object object2 = new Object();
result = (object1 == object2);
System.out.println("Object comparison with == operator: " + result);
result = object1.equals(object2);
System.out.println("Object comparison with equals() method: " + result);
object1 = object2;
result = (object1 == object2);
System.out.println("Object comparison pointing to same Object with == operator: " + result);

Object comparison with == operator: false
Object comparison with equals() method: false
Object comparison pointing to same Object with == operator: true

Points to remember

  • use == to compare primitive e.g. boolean, int, char etc, while use equals() to compare objects in Java.
  • == return true if two reference are of same object. Result of equals() method depends on overridden implementation.
  • For comparing String use equals() instead of == equality operator.
That’s all on difference between equals() method and == operator in Java. Both can compare objects for equality but equals() is used for logical and business logic comparison while == mostly for object reference comparison in Java.

Please post your comments and feedback about the article and its content. If you have any doubt, please feel free to ask it.