参见英文答案 >
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()如何运作?