Wednesday, December 4, 2019

Difference Between equals() Method And equality Operator == in Java

Difference between equals() method and equality operator “==” in Java 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.

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 “==”.

  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.

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.

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

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

You may also like-

  1. Java pass by value or pass by reference
  2. Access modifiers in Java
  3. How HashMap internally works in Java
  4. Fail-Fast Vs Fail-Safe Iterator in Java
  5. Nested Try Statements in Java Exception Handling
  6. Marker interface in Java
  7. Java Multi-Threading Interview Questions
  8. Lambda expressions in Java 8