java Object中hashCode的实现是什么? [英] What's the implementation of hashCode in java Object?
问题描述
可能重复:
如何用Java计算hashCode()
我发现Java中根类 Object
的 hashCode()
方法没有实现:
I found there's no implementation in hashCode()
method of root class Object
in Java:
public native int hashCode();
如果我有对象a
和对象b
,我怎么知道 a.hashCode()
和 b.hashCode()
不使用 System.out.println()的值
?只是通过 hashCode
实现。
If I have an Object a
and an Object b
, how can I know the a.hashCode()
and b.hashCode()
value without using System.out.println()
? Just by the hashCode
implementation.
我试过新的
两个 ArrayList
对象,令我惊讶的是 hashCode()
值是相同的:两者都是1.
I have try to new
two ArrayList
objects and to my big surprise the hashCode()
values are the same: both of them are 1.
推荐答案
hashCode
是一个 native
方法,意思是在内部调用系统库。有关更多详细信息,请参见 Java Native Interface 。
hashCode
is a native
method which means that a system library is called internally. See Java Native Interface for more details.
有一个关于SO的问题为什么hashCode()和getClass()是原生方法?你可能会感兴趣。
There is a question on SO Why hashCode() and getClass() are native methods? Might be interesting for you.
这篇关于java Object中hashCode的实现是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!