I continuously run this program on the same machine:
class Test {
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.hashCode());
}
}
The result is the same each time I run the program on my machine (Windows 7 64 bit): 4384790
On another machine (Windows server 2008, 64 bit), most times it gives me :1671711. However some times the result is : 11394033.
On my machines I run the program under Java HotSpot(TM) Client VM 1.6.0_26/1.6.0_33
I have read the documentation for Object class:
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
But if hashCode() is indeed implemented by converting the internal address of the object into an integer, I still do not understand why the Java VM assigns the same address for the Test object each time I run it.
Is is known exactly how the default hashCode() operates?