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.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); Output: 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); Output: 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.
Please post your comments and feedback about the article and its content. If you have any doubt, please feel free to ask it.
0 comments