Why do you need to override equals() method in Java?
Why do you need to override equals() method in Java?
The equals method of java.lang.Object acts just like the == operator in that they both check to see if two objects are identical. Two object are identical if they both refer to the same instance of a class, that is, they share the same address in memory. This is generally known as an object identity comparison, but is also referred to as a pointer comparison.
The inherent contract of the equals method for objects that extend/inherit from Object class is that it tests for object equality rather than identity. Two objects are said to to be equivalent if they are both instances of the same Type and if the value of each field of the first object is equal to the value of the same field in the second object. Essentially this means we are recursively calling the equals method for each attribute on the objects. Java provides an implementation of equals for the basic/wrapper Types of String, Boolean, Integer, Double, BigDecimal, etc… and all primitives use the == operator for comparison.
See the original post for more information.


Leave a Reply