3

可能重复:
什么是对象 hashcode
hashCode() 和 identityHashCode() 如何在后端工作?

我不是在谈论 String 类或任何其他覆盖哈希码的类。假设我只是创建了一个Object类的新对象,那么hashcode()or 无论如何都会identityHashCode(Object x)返回该对象的内存地址?

4

4 回答 4

7

不必要。从文档(强调我的):

在合理可行的情况下,由 Object 类定义的 hashCode 方法确实为不同的对象返回不同的整数。(这通常通过将对象的内部地址转换为整数来实现,但 JavaTM 编程语言不需要这种实现技术。)

于 2012-06-06T15:55:47.107 回答
1

不,HashCode() 函数返回一个整数。如果你没有为你的对象定义一个 HashCode() 函数,Java 可以将对象的内存地址转码为一个整数并返回它。

于 2012-06-06T16:02:57.413 回答
1

You can always check by looking at the source that ships with your JDK.

My java.lang.Object shows hashCode as a native method. Here are the 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();
于 2012-06-06T15:57:54.243 回答
0

As the documentation on Object.hashCode() states,

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.)

So, the Java language has no requirement for the hashcode of the Object class to return the memory address of the object and therefore you should not rely on this.

于 2012-06-06T15:59:49.100 回答