Reputation: 64783
I am curious how java generates hash values by using hashCode() method of the Object API ?
Upvotes: 20
Views: 35754
Reputation: 15413
The hashCode
method outputs a numeric value. The hashcode for an object is always the same if the object doesn't change. It's important to mention, that it's not have to be unique.
Default implementation of hashCode() is given in such a way that it returns the Hash Code number for the object based on the address of the object. JVM automatically generates this unique number based on the address of the object.
If two objects are at same memory location (the same object referred by two reference variable) then hashcode number for both will be same but if two objects reside at different memory location then hashcode number for both object will be different.
You probably asked the question because you need to override the method. But, when to override it?
Default implementation of hashCode() method returns hashcode number (unique identity of object) based on the address of the object. But if my application requires to uniquely identify the objects based on some different parameter (rather than address of the object), then I should override hashCode() method and should give implementation as per the requirement.
An important note from Effective Java
about the topic:
For more, check out this Java hashcode example.
Upvotes: 0
Reputation: 131
The HashCode () function has several options for creating a hash code. It sets the JVM startup parameter. Function, which create hashCode() written on C++, and you can see code here
The information was taken from here
Upvotes: 3
Reputation: 533432
The Object.hashCode() uses the System.identityHashCode() which is based on an id number for a given object.
Upvotes: 4
Reputation: 5513
According to Java Platform API documentation, the calculation of hashcode is based on 32-bit internal JVM address of the Object.
It is true that the object moves during execution (AFAIK the only reason is garbage collector). But hashcode does not change.
So when you have an object like this
Person person1 = new Person();
person1.setName("Alex");
Person person2 = new Person();
person2.setName("Alex");
Person person3 = person2;
In this case person1.hashCode will not be equal to person2.hashCode because the memory addresses of these two objects are not the same.
But person2.hashCode will be equal to person3 because they are pointing to the same object.
So if you need to use hashCode method for your objects you must implement it yourself.
By the way String.hashCode implementation is different. It is something like this: (C# syntax)
public int hashCode(String str)
{
int h = 0;
for (int i = 0; i < str.Length; i++)
h = (h * 31) + str[i];
return h;
}
edit: No overflow check is done here, so the hashCode may be positive or negative.
Upvotes: 6
Reputation: 41077
Java does not generate meaningful hashCode
for you, it is your job as a programmer to generate a useful hashCode
. The default hashCode
is just the memory location.
Upvotes: 1
Reputation: 570285
The hashCode()
of Object
is actually a native method and the implementation is actually not pure Java. Now, regarding the how it works, this answer from Tom Hawtin does a great job at explaining it:
Many people will claim that
Object.hashCode
will return the address of the object representation in memory. In modern implementations objects actually move within memory. Instead an area of the object header is used to store the value, which may be lazily derived from the memory address at the time that the value is first requested.
The whole answer is actually worth the read.
Upvotes: 25
Reputation: 16530
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).
Upvotes: 10