In Java, two objects can be compared using the equals()
method and == operator.
== operator is used to check if the references to two
objects are same i.e. they point to the same location in memory.
The equals() method is used to check if the two
objects have the same contents (same values in attributes). The
default equals() implementation is provided in the Object class and its
behavior is exactly the same as the == operator. A programmer is
expected to override the equals() method and compare object contents to get the
desired behavior. Another point to be noted is that hashCode() and equals()
method should always be overridden together.
No comments:
Post a Comment