hashcode() java,hashCode()是如何在Java中实现的

How is hashCode() implemented?

My assumption is that it uses the object memory location as the initial number (the seed) on which it runs the hash function. However, this is not the case.

I've also looked at Hash : How does it work internally? but it does not answer my question.

Yes I could download the SDK, but before I do that and look at the code, perhaps someone else already has knowledge of it.

Thanks :)

EDIT:

I know it should be overridden and such, so please try to stay on topic :)

解决方案

Of course it is implementation specific, but generally the hash code for an object will be computed lazily and stored in the object header. Odd things are done with headers to keep them small whilst allowing complex locking algorithms.

In the OpenJDK/Oracle JVM the usual method of computing the initial hash code is based on the memory address at the time of the first request. Objects move about in memory, so using the address each time would not be a good choice. The hash code isn't the actual address - that would typically be a multiple of eight which isn't great for using straight in a hash table particularly with a power of two size. Note identity hash codes are not unique.

HotSpot has build time options to always use zero or use a secure random number generator (SRNG) for testing purposes.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值