Difference between equals() method and equality operator “==” in Java is a frequently asked Java interview question. At the same time it may be a bit confusing for the first-time Java programmer to get the subtle differences between equals and “==”.
So in this post let’s try to find the exact differences between the equals() method and equality operator “==” in Java and where does it make sense to use what.
Java equality operator “==”
equality operator “==” in Java can be used to compare primitive values as well as objects. Though it works just fine with primitive values but in case of objects “==” will compare the references of the objects not the content of the objects.
equals() method in Java
If you want to compare the actual contents of the objects then you will have to override equals method, which is present in Object class so available to all the other classes. Default implementation of equals() in the Object class compares using == operator, so default implementation will compare references.
You will have to override equals method in order to compare your custom class objects.
Difference between equals and “==” in Java
Based on what we have seen so far we can surely list some difference between equals and equality operator “==”.
- “==” is an operator where as equals is a method.
- “==” can be used with primitive values as well as with objects. Equals method can only be used with objects.
- When “==” is used for comparing objects it will compare their references not the actual content. Equals method can compare the actual content of the object but you will have to provide your own implementation for determining the equality of two objects.
Java example using “==” operator and equals()
Let’s move on to see some hands-on code to test the theory stated so far.
public class EqualsMethodDemo { public static void main(String[] args) { String str1 = new String("Test"); String str2 = new String("Test"); System.out.println(str1 + " == " + str2 + " - " + (str1 == str2)); System.out.println(str1 + ".equals(" + str2 + ") - " + str1.equals(str2)); } }
Output
Test == Test - false Test.equals(Test) - true
Here two string objects, having the same content, are created. Now comparison of these two strings is done using equality “==” operator and .equals() method.
It can be seen that “==” returns false even though the content of both the string is same. That is because references of both the strings are different. Where as comparison using .equals() returns true.
Here note that String class as well as wrapper classes like Integer, Long provide implementation of equals method which compares the content. Same way for your own classes you will have to provide your own implementation of equals method.
- Refer Overriding hashCode() and equals() method in Java to see how to provide implementation of equals method in your classes.
Integer class equals method example
As mentioned above Integer class provides implementation of equals method so that too will compare the content when equal method is used.
public class EqualsMethodDemo { public static void main(String[] args) { Integer num1 = new Integer(7); Integer num2 = new Integer(7); System.out.println(num1 + " == " + num2 + " - " + (num1 == num2)); System.out.println(num1 + " == " + num2 + " - " + num1.equals(num2)); } }
Output
7 == 7 - false 7 == 7 - true
That's all for this topic Difference Between equals() Method And equality Operator == in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment