java 默认hashcode,java – hashCode()的默认行为

参见英文答案 >

Why hashCode() returns the same value for a object in all consecutive executions?                                    8个

我在同一台机器上连续运行这个程序:

class Test {

public static void main(String[] args) {

Test test = new Test();

System.out.println(test.hashCode());

}

}

每次在我的机器上运行程序(Windows 7 64位)时结果都是一样的:

4384790

在另一台机器上(Windows server 2008,64位),大多数时候它给了我:1671711.但有时候结果是:11394033.

在我的机器上,我在Java HotSpot(TM)Client VM 1.6.0_26 / 1.6.0_33下运行该程序

我已经阅读了Object类的文档:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This

is typically implemented by converting the internal address of the

object into an integer, but this implementation technique is not

required by the JavaTM programming language.)

但是如果确实通过将对象的内部地址转换为整数来实现hashCode(),我仍然不明白为什么每次运行它时Java VM都为Test对象分配相同的地址.

是否确切知道默认的hashCode()如何运作?

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值