hashCode()方法在java中返回什么值?
我读到它是一个对象的内存引用...当我打印哈希值为new Integer(1)1时; 因为String("a")是97.
我很困惑:它是ASCII还是什么类型的值?
hashCode()HotSpot 的默认实现返回一个随机值并将其存储在对象头中.这在Java 8中似乎没有改变,其中哈希值是通过调用来计算的os::random():
static inline intptr_t get_next_hash(Thread * Self, oop obj) {
intptr_t value = 0 ;
if (hashCode == 0) {
// This form uses an unguarded global Park-Miller RNG,
// so it's possible for two threads to race and generate the same RNG.
// On MP system we'll have lots of RW access to a global, so the
// mechanism induces lots of coherency traffic.
value = os::random() ;
} else …Run Code Online (Sandbox Code Playgroud) JVM实现java.lang.Object隐式hashCode()方法时使用的算法是什么?
[ OpenJDK或者Oracle JDK在答案中是首选].