What value is hashCode() method is returning in java?. i read that it is an memory reference of an object. when i print hascode value for new Integer(1), its 1. for String(a) - 97. so i confused. is it ascii or what type of value is?
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.
The value returned by hashCode()
is by no means guaranteed to be the memory address of the object. I'm not sure of the implementation in the Object
class, but keep in mind most classes will override hashCode()
such that two instances that are semantically equivalent (but are not the same instance) will hash to the same value. This is especially important if the classes may be used within another data structure, such as Set, that relies on hashCode
being consistent with equals
.
If you want a hashCode()
that uniquely identifies an instance no matter what, use System.identityHashCode()
- this will delegate to the default hashCode
method regardless of whether it has been overridden.
The hashCode()
method is often used for identifying an object. I think the Object
implementation returns the pointer (not a real pointer but a unique id or something like that) of the object. But most classes override the method. Like the String
class. Two String objects have not the same pointer but they are equal:
new String("a").hashCode() == new String("a").hashCode()
I think the most common use for hashCode()
is in Hashtable
, HashSet
, etc..
Object.hashCode(), if memory serves correctly (check the JavaDoc for java.lang.Object), is implementation-dependent, and will change depending on the object (the Sun JVM derives the value from the value of the reference to the object).
Note that if you are implementing any nontrivial object, and want to correctly store them in a HashMap or HashSet, you MUST override hashCode() and equals(). hashCode() can do whatever you like (it's entirely legal, but suboptimal to have it return 1.), but it's vital that if your equals() method returns true, then the value returned by hashCode() for both objects are equal.
Confusion and lack of understanding of hashCode() and equals() is a big source of bugs. Make sure that you thoroughly familiarize yourself with the JavaDocs for Object.hashCode() and Object.equals(), and I guarantee that the time spent will pay for itself.
Object.hashCode() used to return a memory address about 14 years ago. Not since.
If you want to know how they are implmented, I suggest you read the source. If you are using an IDE you can just + on a method you are interested in and see how a method is implemented. If you cannot do that, you can google for the source.
For example, Integer.hashCode() is implemented as
public int hashCode() {
return value;
}
and String.hashCode()
public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}