.equals and == in java

Both equals() and == are used in object comparison to check the quality of the object, but actually if we see equals is a method whereas == is an operator. As, equals is a method the logic of comparing the object can be changed based upon the business logic. But, since java does not supports operator overloading “==” acts identical for all objects.

There are many more differences between equals() and ==. Java beginners face a lot of issue in figuring out when they should use equals() and when ==. In this tutorial, I will clear these doubts step by step.

What is “==”/equality operator

For comparing all primitives as well as objects "==" or the equality can be used. When comparing primitive data type the “==” or equality operator is straight forward, but when it comes to comparing the objects it creates confusion with equal().

The equality operator or "==" compares two objects based on a memory reference so, the "==" operator will return true only if the two object references being compared represent the exact same object otherwise "==" will return false.

What is equals method

equals() is one of the default method in object class of java. Using this method we can check the equality of two object which are based on business logic. For example, two Students are considered as equal if they have same studentId.

In the class we can override equals method and can give the logic on which the two objects will be compared based upon the business needs. One important thing that we need to take care is that equals has a contract with hashcode() method, which states that:

· If two objects are equal according to equals method then there hashcode should also be same.

· If the hashcode method returns the same hashcode for two objects then they may or may not be equal.

Hence, it is compulsory to override hashcode() along with equal().

Using == and equals for String comparision

String comparision is the most common scenerio in which we use == and .equals. The java.lang.String class overrides equals method by default. equals() method will return true if the two string objects have same content on the other hand “==“ will return true if and only if the references point on same object.

Have a look at the example below for more clarity:

Using == and equals for Object Comparision

Another scenario causing confusion between == and .equals methods is when you are comparing two objects. If you compare two references that refer to an object of type object, you should see the same result of both == operator as equal method, because standard implementation of a equals method only compares the memory address of two objects and return true if same object is refered by both the reference variables.

Have a look at the example below for more clarity:


Conclusion:

                1. While comparing primitive like Boolean, int, float etc use “==” and to compare objects use .equals().

                2. For String comparisons use equals() instead of “==” operator.


Related Articles:

        1.      Core java interview questions.

        2.      Java 8 interview questions.

        3.      Internal working of Hashmap in java.

        4.      Abstractionin Java.

        5.      Encapsulation in java.

        6.      Compile time polymorphism in java.

Post a Comment

0 Comments