我查了元代码,有如下描述:对toString(): The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character @, and the unsigned hexadecimal representation of the hash code of the object.对hashCode(): Returns a hash code value for the object. Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals 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. 关于hashCode方法,更详细的描述可以参考http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html由此可见,@后面的hash code不是什么内存地址,而是标示对象的一个int值.!!!!native的hashCode()的代码在哪里?
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}3. 根据 Sun 提供的 JDK 源代码,Object.hashCode() 是下面这个样子: public native int hashCode();4. 至于 native 的 hashCode() 内部怎么实现,也能从 JDK 的源代码里找到,我没仔细看,但恐怕不只是“内存地址”这么简单;
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character @, and the unsigned hexadecimal representation of the hash code of the object.对hashCode():
Returns a hash code value for the object. Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals 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.
关于hashCode方法,更详细的描述可以参考http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html由此可见,@后面的hash code不是什么内存地址,而是标示对象的一个int值.!!!!native的hashCode()的代码在哪里?
(我没有仔细看到最后,太累了,但已经可以肯定,hashCode 并不是简单的取内存地址)<JDK142_01-src>\j2se\src\share\native\java\lang\Object.c
static JNINativeMethod methods[]<JDK142_01-src>\hotspot\src\share\vm\prims\jvm.cpp
JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))<JDK142_01-src>\hotspot\src\share\vm\oops\oop.inline.hpp
inline intptr_t oopDesc::identity_hash()<JDK142_01-src>\hotspot\src\share\vm\oops\oop.cpp
intptr_t oopDesc::slow_identity_hash()<JDK142_01-src>\hotspot\src\share\vm\runtime\synchronizer.cpp
intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) ……