Will .hashcode() return a different int due to compaction of tenure space?

前端 未结 5 1718
谎友^
谎友^ 2020-12-01 01:24

If I call the Object.hashcode() method on some object it returns the internal address of the object (default implementation). Is this address a logical or phys

5条回答
  •  死守一世寂寞
    2020-12-01 01:57

    No, the default hash code of an object will not change.

    The documentation doesn't say that the hash code is the address, it says that it is based on the address. Consider that hash codes are 32 bits, but there are 64-bit JVMs. Clearly, directly using the address wouldn't always work.

    The implementation depends on the JVM, but in the Sun (Oracle) JVM, I believe the hash code is cached the first time it's accessed.

提交回复
热议问题