Monday, 12 June 2017

Difference between equals() method and equality operator == in Java

Difference between equals() method and equality operator “==” is a frequently asked 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 “==” and where does it make sense to use what.

Equality operator “==”

equality operator “==” 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

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. Though the implementation in the Object class is to compare using == operators 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 “==”

Based on what we have seen so far we can surely list some difference between equals and equality operator “==”.

  1. “==” is an operator where as equals is a method.
  2. “==” can be used with primitive values as well as with objects. Equals method can only be used with objects.
  3. 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.

Example code

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 = "Test";
  String str2 = new String("Test");
  
  System.out.println(str1 + " == " + str2 + " - " + (str1 == str2));
  
  System.out.println(str1 + " != " + str2 + " - " + (str1 != str2));

 }
}

Output

Test == Test - false
Test != Test – true

Here two strings are created; first one as a string literal and second using new operator so that reference for both the strings are different. Now comparison of these two strings is done using equality “==” operator and not equal (“!=”) operator.

It can be seen that equal returns false even though the content of both the string is same. That is because references of both the strings are different.

Code using equals method

Now if you use equals method to compare the strings then the class used above can be rewritten as -

public class EqualsMethodDemo {

 public static void main(String[] args) {
  String str1 = "Test";
  String str2 = new String("Test");
  
  System.out.println(str1 + " == " + str2 + " - " + str1.equals(str2));
 }
}

Output

Test == Test - true

Now the output is true as equals method is comparing the content of both the strings.

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.

Code with Integer

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!


Related Topics

  1. Equality and Relational Operators in Java
  2. Conditional Operators in Java
  3. String comparison in Java
  4. Overriding hashCode() and equals() method in Java

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment