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

后端 未结 5 1678
慢半拍i
慢半拍i 2020-12-17 07:40

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 6

5条回答
  •  星月不相逢
    2020-12-17 07:49

    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)

提交回复
热议问题