I'm trying to understand the native implementation of the hashCode() method. What exactly does this method return? Is it a memory address or is it a random value?
- 
        4The OpenJDK sources should tell you. I doubt it's a memory address since the GC can move objects around in memory, it's probably some sort of internal object handle.millimoose– millimoose2013-04-19 13:11:59 +00:00Commented Apr 19, 2013 at 13:11
- 
        This is an excerpt of said code from around 2008: blogs.tedneward.com/…millimoose– millimoose2013-04-19 13:13:23 +00:00Commented Apr 19, 2013 at 13:13
- 
        And straight from the horse's mouth: hg.openjdk.java.net/jdk7/hotspot/hotspot/file/9b0ca45cd756/src/… The code looks the same to me as in the blog post.millimoose– millimoose2013-04-19 13:39:51 +00:00Commented Apr 19, 2013 at 13:39
- 
        2possible duplicate of What is an "internal address" in Java?assylias– assylias2013-04-19 13:42:55 +00:00Commented Apr 19, 2013 at 13:42
3 Answers
.hashCode() native implementation depends on JVM. 
E.g. HotSpot has 6 Object.hashCode() implementations. You can choose it using -XX:hashCode=n flag running JVM via command line, where n:
0 – Park-Miller RNG (default)
1 – f(address, global_statement)
2 – constant 1
3 – Serial counter
4 – Object address
5 – Thread-local Xorshift
1 Comment
From the documentation:
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.)
So it may be related to a memory address, but it doesn't have to be - and you definitely shouldn't make any assumption about it being related to memory at all.
Nothing you do with a hash code should care about this at all. The only things you should infer from hash codes are:
- If the hash codes of two objects are the same, they may be equal objects
- If the hash codes of two objects are different, they are not equal objects (assuming a correct implementation, whether overridden or not)
7 Comments
Your answer lies here. As mentioned in the documentation:
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.)




