Search code examples
javahashobjecthashtable

how does the hashCode() method of java works?


I am curious how java generates hash values by using hashCode() method of the Object API ?


Solution

  • Java doesn't generate hashCode(), i.e. nothing automatic is happening here. However, Object generates a HashCode based on the memory address of the instance of the object. Most classes (especially if you are going to use it in any of the Collection API) should implement their own HashCode (and by contract their own equals method).