Object.hashCode() algorithm

[亡魂溺海] 提交于 2019-11-29 05:59:20

Native hashCode method implementation depends on the JVM. By default in HotSpot it returns random number, you can check it in the source code (function get_next_hash)

Despite the Javadoc, the algo only may use the address as an input. This means that even though new objects use the same address in eden space they won't have the same hashCode.

There is a number of algos it might be using and not all use the address.

Note: the hashCode() is 31-bit.

BTW You can set it with Unsafe.putInt(object, 1, value)on Hotspot.

Set<Integer> ints = new LinkedHashSet<>();
int negative = 0, nonneg = 0;
for (int i = 0; i < 100; i++) {
    System.gc();
    for (int j = 0; j < 100; j++) {
        int h = new Object().hashCode();
        ints.add(h);
        if (h < 0) negative++;
        else nonneg++;
    }
}
System.out.println("unique: " + ints.size() + " negative: " + negative + " non-neg: " + nonneg);

prints

unique: 10000 negative: 0 non-neg: 10000

Using Unsafe

Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafe.get(null);

Object o = new Object();
System.out.println("From header " + Integer.toHexString(unsafe.getInt(o, 1L)));
// sets the hashCode lazily
System.out.println("o.hashCode()  " + Integer.toHexString(o.hashCode()));
// it's here now.
System.out.println("after hashCode() From header " + Integer.toHexString(unsafe.getInt(o, 1L)));
unsafe.putInt(o, 1L, 0x12345678);
System.out.println("after change o.hashCode()  " + Integer.toHexString(o.hashCode()));

prints

From header 0
o.hashCode()  2260e277
after hashCode() From header 2260e277
after change o.hashCode()  12345678

hashCode is a native method, which means that a system library is called internally. This is because of the reason that hashcode internally will try to generate a number depending on the object memory location. This code is machine dependent and probably written in C.

But if you are really interested to see the native code, then follow this:

http://hg.openjdk.java.net/jdk7/jdk7-gate/jdk/file/e947a98ea3c1/src/share/native/java/

It's because it relies on low-level details that aren't exposed to Java code. Some basic parts of the standard library (like java.lang.Object) must be implemented in native code.

As an aside, you can find at least one interesting article that does into more detail about the HotSpot implementation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!