Have a Question?

If you have any question you can ask below or enter what you are looking for!

What is the Contract between equals and hashCode method?

What is the Contract between equals and hashCode method?
  • hashCode() method provides the hashcode of an object. Basically, the default implementation of hashCode() provided by Object is derived by mapping the memory address to an integer value. If you look into the source of Object class, you will find the following code for the hashCode.
  • equals() method is used to make the equal comparison between two objects. There are two types of comparisons in Java. One is using “= =” operator and another is “equals()”. So in the broad sense, you say that two objects are equivalent they satisfy the “equals()” condition.
  • Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
Tags:  , , , ,

Leave a Reply

Your email address will not be published.