Java 哈希代码是否返回内存地址?
可能重复:Java 哈希代码是否返回内存地址?,java,hashcode,Java,Hashcode,可能重复: 我不是在谈论字符串类或任何其他覆盖hashcode的类。假设我刚刚创建了对象类的一个新对象,那么hashcode()或者在任何情况下,identityHashCode(object x)会返回该对象的内存地址吗 不一定。从(我的)重点: 只要是合理可行的,类对象定义的hashCode方法确实会为不同的对象返回不同的整数。(这通常是通过将对象的内部地址转换为整数来实现的,,但是JavaTM编程语言不需要这种实现技术 您可以通过查看JDK附带的源代码进行检查 Myjava.lang.
我不是在谈论字符串类或任何其他覆盖hashcode的类。假设我刚刚创建了
对象
类的一个新对象,那么hashcode()
或者在任何情况下,identityHashCode(object x)
会返回该对象的内存地址吗 不一定。从(我的)重点:
只要是合理可行的,类对象定义的hashCode方法确实会为不同的对象返回不同的整数。(这通常是通过将对象的内部地址转换为整数来实现的,,但是JavaTM编程语言不需要这种实现技术
您可以通过查看JDK附带的源代码进行检查 My
java.lang.Object
将hashCode
显示为本机方法。这里是javadocs
/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hashtables such as those provided by
* <code>java.util.Hashtable</code>.
* <p>
* The general contract of <code>hashCode</code> is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the <tt>hashCode</tt> method
* must consistently return the same integer, provided no information
* used in <tt>equals</tt> comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* <li>If two objects are equal according to the <tt>equals(Object)</tt>
* method, then calling the <code>hashCode</code> method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the <tt>hashCode</tt> method on each of the
* two objects must produce distinct integer results. However, the
* programmer should be aware that producing distinct integer results
* for unequal objects may improve the performance of hashtables.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class <tt>Object</tt> 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
* Java<font size="-2"><sup>TM</sup></font> programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.util.Hashtable
*/
public native int hashCode();
正如Object.hashCode()上的语句所述
只要是合理可行的,类对象定义的hashCode方法确实会为不同的对象返回不同的整数。(这通常通过将对象的内部地址转换为整数来实现,但JavaTM编程语言不需要这种实现技术。)
因此,Java语言不要求对象类的hashcode返回对象的内存地址,因此您不应该依赖它。否,hashcode()函数返回一个整数。如果尚未为对象定义HashCode()函数,Java可能会将对象的内存地址转换为整数并返回该整数。有趣的是,
HashCode
是一个int,计算机上的地址空间很可能大于可用的int数,并且将内部地址转换为int不一定是双射。“这通常是…”意味着,有没有其他实现方法?@assylias,不一定是双射。。。但不管怎么说,这都无关紧要。@user601L,实际上它已经不是(jnit)(void*)地址了很久了,实际的impl。在对象头中存储一个一次性随机数。因此,正如文档所说,“如果根据equals(java.lang.object)方法,两个对象不相等,那么对两个对象中的每一个调用hashCode方法都必须产生不同的整数结果,这不是必须的。”如果hashcode方法没有被覆盖,则不是真的吗?我很确定它在OpenJDK上不会被覆盖,否则我们将在连续创建的对象的identityHashCode()
中看到更多的模式。(试一试,创建100个对象,打印它们的哈希代码,并尝试查找模式)。最后我查看了一下,如果特定类没有覆盖它,那么默认实现是返回一个从首次请求哈希代码时的对象地址派生的整数。当然,对于复制GC,地址可能会更改,因此实现(如果它使用地址)必须小心缓存首先生成的哈希代码,并始终返回该代码,而不是重新派生它。没有要求哈希代码与地址有任何关系,对于某些对象(例如字符串),它肯定不会基于地址,因为相等的对象必须具有相等的哈希。下面是一个关于如何进行的链接:@HotLicks,有人真的必须修复无意义的文档,这只是误导。有时这些胡说八道是故意的。它可能会使用内存地址,而不是必须使用。感谢您的更正,已更新。@Robin,它从早期的impl开始就没有返回地址。在thread.cpp和synchronizer.cpp::get_next_散列,第270行中有本机代码。我无法想象为什么三年后它被否决了。了解如何查看Java源代码是有价值的。你认为javadocs来自哪里?