问题描述
Object.hashCode() 的默认实现通常是对象在内存中的分配地址的一些函数(尽管这不是由 JLS).鉴于 VM 在内存中分流对象,为什么 System.identityHashCode() 返回的值在对象的生命周期内永远不会改变?
Typically the default implementation of Object.hashCode() is some function of the allocated address of the object in memory (though this is not mandated by the JLS). Given that the VM shunts objects about in memory, why does the value returned by System.identityHashCode() never change during the object's lifetime?
如果是一次性"计算(对象的 hashCode 计算一次并存储在对象头或其他东西中),那么这是否意味着两个对象有可能具有相同的 identityHashCode (如果它们恰好首先分配在内存中的相同地址)?
If it is a "one-shot" calculation (the object's hashCode is calculated once and stashed in the object header or something), then does that mean it is possible for two objects to have the same identityHashCode (if they happen to be first allocated at the same address in memory)?
推荐答案
现代 JVM 将值保存在对象头中.我相信该值通常仅在首次使用时计算,以便将对象分配所花费的时间保持在最低限度(有时低至十几个周期).可以编译通用的 Sun JVM,使所有对象的标识哈希码始终为 1.
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.
这篇关于JVM 如何确保 System.identityHashCode() 永远不会改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)