How to calculate identity hashCode is JVM specific.
As to HotSpot JVM, it uses random number generator for initial calculation of identity hashCode. After the hashCode is calculated for the first time, it is saved in the object header so that subsequent calls to identityHashCode
will return the same value.
Actually the algorithm of generating identity hashCode can be altered by -XX:hashCode
JVM option. Look at the sources. There are the following options:
-XX:hashCode=0
- global Park-Miller RNG (default until Java 7)
-XX:hashCode=1
- function(obj_address, global_state)
-XX:hashCode=2
- constant 1. All objects will have the same hashCode. Just for testing.
-XX:hashCode=3
- incremental counter.
-XX:hashCode=4
- lower 32 bits of the object address in the Heap
-XX:hashCode=5
- thread-local Marsaglia's Xor-shift RNG (default since Java 8)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…