Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

java - What's the default hash value of an object on 64 bit JVM

Since default hash value of an object is the object address of the object, on 32-bit machine, it makes sense considering hash value is an int value. My question is that on 64 bit machine, address is supposed to be 64 bit right? Then what about the 32 bit int hash value? Is there going to be some down conversion(from 64bit to 32 bit)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...