Java, Object.hashCode() result constant across all JVMs/Systems?

前端 未结 6 1354
傲寒
傲寒 2020-12-05 21:17

Is the output of Object.hashCode() required to be the same on all JVM implementations for the same Object?

For example if \"test\".hashCode()

6条回答
  •  执念已碎
    2020-12-05 21:42

    No. The output of hashCode is liable to change between JVM implementations and even between different executions of a program on the same JVM.

    However, in the specific example you gave, the value of "test".hashCode() will actually be consistent because the implementation of hashCode for String objects is part of the API of String (see the Javadocs for java.lang.String and this other SO post).

提交回复
热议问题