`hashCode`的默认实现是什么? [英] What is the default implementation of `hashCode`?
问题描述
如果没有覆盖 hashCode
方法,那么 hashCode
的默认实现是什么?
If one does not override the hashCode
method, what is the default implementation of hashCode
?
推荐答案
然后这个类从其祖先之一继承 hashCode
。如果没有覆盖它,那么使用了Object.hashCode 。
Then this class inherits hashCode
from one of its ancestors. If non of them overrides it, then Object.hashCode is used.
来自 docs :
As在合理实际的情况下,由Object类定义的hashCode方法确实为不同的对象返回不同的整数。 (这通常通过将对象的内部地址转换为整数来实现,但JavaTM编程语言不需要此实现技术。)
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.)
因此默认实现是特定于JVM的
So default implementation is JVM-specific
这篇关于`hashCode`的默认实现是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!