When i see the implementation of equals() method it does nothing but same as what == does. So my question is what was the need to have this as separate method when we have == operator which does the same work?
When i see the implementation of equals() method it does nothing but same as
Share
You can not overload the
==operator, but you can overrideequals(Object)if you want it to behave differently from the==operator, i.e. not compare references but actually compare the objects (e.g. using all or some of their fields).Also, if you do override
equals(Object), have a look athashCode()as well. These two methods need to be compatible (i.e. two objects which are equal according toequals(Object)need to have the samehashCode()), otherwise all kinds of strange errors will occur (e.g. when adding the objects to a set or map).