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.

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 primitives 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 –

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.

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

Output

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.

Output

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

One Comment

  1. Main difference between == and equals in Java is that “==” is used to compare primitives while equals() method is recommended to check equality of objects.
    By the way! The best essay writing service – https://www.easyessay.pro/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.