How is hashCode() calculated in Java

Jothi picture Jothi · Mar 11, 2010 · Viewed 84.8k times · Source

What value does the hashCode() method return in java?

I read that it is a memory reference of an object... The hash value for new Integer(1) is 1; the hash value for String("a") is 97.

I am confused: is it ASCII or what type of value is?

Answer

Andrew Hare picture Andrew Hare · Mar 11, 2010

A hashcode is an integer value that represents the state of the object upon which it was called. That is why an Integer that is set to 1 will return a hashcode of "1" because an Integer's hashcode and its value are the same thing. A character's hashcode is equal to it's ASCII character code. If you write a custom type you are responsible for creating a good hashCode implementation that will best represent the state of the current instance.