Why hashcode() method in Java return int value instead of long

In Java hashCode() method could have returned the long value to maintain wide range of unique hashcode, but as per design contract hashCode method return int value. This is my favorite interview question on hashCode method.

Signature of hashCode() method  –  public native int hashCode();

There is logical reason behind this to return int value instead of long as the maximum size of an array supported in Java is Interger.MAX_VALUE.

Since the main use of hashCode()  method is to determine correct slot to insert an object into in the backing array of a HashMap/Hashtable, a hashcode > Integer.MAX_VALUE would not be able to be stored in the array.

Happy Learning 🙂



You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *