Java,Object.hashCode()结果是否在所有JVM/系统中都为常数? [英] Java, Object.hashCode() result constant across all JVMs/Systems?

查看:105
本文介绍了Java,Object.hashCode()结果是否在所有JVM/系统中都为常数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在同一对象的所有JVM实现上,Object.hashCode()的输出是否必须相同?

例如,如果"test".hashCode()在1.4上返回1,它是否有可能返回在1.6上运行的2.或者,如果操作系统不同,或者实例之间存在不同的处理器体系结构,该怎么办?

解决方案

否. hashCode的输出可能会在JVM实现之间甚至在同一JVM上的程序的不同执行之间发生变化.

但是,在您给出的特定示例中,"test".hashCode() 的值实际上是一致的,因为hashCode对象的hashCode的实现是String的API的一部分(请参阅 java.lang的Javadocs.字符串其他SO帖子).

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

For example if "test".hashCode() returns 1 on 1.4, could it potentially return 2 running on 1.6. Or what if the operating systems were different, or there was a different processor architecture between instances?

解决方案

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).

这篇关于Java,Object.hashCode()结果是否在所有JVM/系统中都为常数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆