June 16, 2022

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

Difference between equals() method and equality operator "==" in Java is asked quite frequently in beginner level Java interviews. Since both equals() and == operator are used for comparison so it is necessary to know the differences between these two in order to ensure correct usage of one of them as per scenario.

Equality operator "==" in Java

Equality operator in Java is used to compare two primitive values or objects to test if the compared primitive values or objects are equal or not. In case of primitive types like int, long, float, "==" operator works fine and compares the values of the variables but in case of objects, equality operator compares the object references not their values. For example-

int a = 7;
int b = 8;
if(a == b){
  System.out.println("Values are same");
}else {
  System.out.println("Values are not same");
}

Here values stored in the variables are compared, since the values are not equal so the if condition fails.

In case of objects– if (obj1 == obj2) equality operator compares the object references i.e. memory location of both the objects not the values. In case of objects “==” operator returns true only if both objects are having the same reference i.e. pointing to the same memory location.

equals method in Java

equals method is defined in the Object class in Java and used for content comparison. The default implementation in the Object class compares using equality operator. This default implementation of the equals method has to be overridden to determine the equality of the custom objects. In your class you will have to override and implement equals method in such a way that it can compare two instances of the class to determine whether both instances are logically equal or not.

For example if you have employee class with fields id, firstName and lastName then using these fields you can write equals method that can compare two Employee objects for equality.

@Override
public boolean equals(Object obj) {
  if (this == obj)
    return true;
  if (obj == null)
    return false;
  if (getClass() != obj.getClass())
    return false;
  Employee other = (Employee) obj;
  if (empId != other.empId)
    return false;
  if (firstName == null) {
    if (other.firstName != null)
      return false;
  } else if (!firstName.equals(other.firstName))
    return false;
  if (lastName == null) {
    if (other.lastName != null)
      return false;
  } else if (!lastName.equals(other.lastName))
    return false;
  return true;
}

Good thing is that wrapper classes in Java like Integer, Float etc. and String class override and provide implementation of equals method so you don’t need to do that for these classes.

Difference between equals() and "==" operator in Java

Since we have a pretty good explanation of equality operator and equals method so lets list out the differences between the two-

  1. First difference between equality operator and equals method is that “==” is an operator in Java where as equals is a method.
  2. Equality operator can be used to compare primitives as well as objects. Equals method can only be used with objects.
  3. Equality operator compares the object references when two objects are compared using equality operator. Equals method when overridden can do content comparison of two objects.

Example using “==” and equals

public class ComparisonDemo {
  public static void main(String[] args) {
    String str1 = new String("Code");
    String str2 = new String("Code");
    
    System.out.println("str1 == str2 " + (str1 == str2));
    System.out.println("str1.equals(str2) " + (str1.equals(str2)));
  }
}
Output
str1 == str2 false
str1.equals(str2) true

In the code there are two String objects str1 and str2. Since both objects have different references so equality operator returns false. Content of both the String objects is same that is why comparison using equals method returns true.

If we use String literals instead of creating new String objects and then compare them. Note that in case of String literals if object with the same value is already there then another literal also uses the same reference.

public class ComparisonDemo {
  public static void main(String[] args) {
    String str1 = "Code";
    String str2 = "Code";
    
    System.out.println("str1 == str2 " + (str1 == str2));
    System.out.println("str1.equals(str2) " + (str1.equals(str2)));
  }
}
Output
str1 == str2 true
str1.equals(str2) true

Since both String literals have the same values so they share the same reference, that is why “==” operator also returns true.

That's all for the topic Difference Between "==" Operator And equals() Method in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment