If Java's garbage collector moves objects, what is Object.hashCode and System.identityHashCode?

后端 未结 3 1713
面向向阳花
面向向阳花 2021-02-02 07:18

I\'ve often heard that these methods (Object.hashCode and System.identityHashCode) return the address of the object, or something computed quickly from

3条回答
  •  不要未来只要你来
    2021-02-02 07:57

    .NET's implementation is intentionally not published (and when you attempt to decompile it, you will find that it makes an unmanaged framework call). The only documentation as such is here, which only states that it is "not guaranteed to produce a different value for each object", and "may change between framework versions". Making any assumptions about how it actually works is probably ill-advised.

    Java's is more well-understood (though presumably could differ across JVMs), and is covered specifically in this question: Will .hashcode() return a different int due to compaction of tenure space?

    The gist of the Java implementation is that by contract, the value of an object's hashcode is not relevant until it is retrieved for the first time. After that, it must remain constant. Thus the GC moving the object doesn't matter until the object's hashcode() method is called for the first time. After that, a cached value is used.

提交回复
热议问题