How does the JVM ensure that System.identityHashCode() will never change?

前端 未结 5 933
终归单人心
终归单人心 2020-11-29 00:33

Typically the default implementation of Object.hashCode() is some function of the allocated address of the object in memory (though this is not

5条回答
  •  执笔经年
    2020-11-29 00:34

    Modern JVMs save the value in the object header. I believe the value is typically calculated only on first use in order to keep time spent in object allocation to a minimum (sometimes down to as low as a dozen cycles). The common Sun JVM can be compiled so that the identity hash code is always 1 for all objects.

    Multiple objects can have the same identity hash code. That is the nature of hash codes.

提交回复
热议问题