Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object.hashCode() algorithm

I'm looking for the algorithm of Object.hashCode().

This code is native in Object.java.

Is this because

(a) the code is in assembly-- never was in Java or any other HLL at all

or

(b) it simply isn't disclosed

?

In either case, I am looking to get hold of the algorithm (pseudo-code or some detailed explanation) of "how hashCode() is calculated"-- what are the params going into its calculation and the calculation itself?

Please note: It's the hashCode() of Object i'm looking for-- not another like that of String or hashMap/table.

//==========================================================================

the new Java docs-- jdk 8 now saying

"The value returned by hashCode() is the object's hash code, which is the object's memory address in hexadecimal." 
like image 865
Roam Avatar asked Jul 31 '13 17:07

Roam


People also ask

What algorithm does Java hashCode use?

The Java hashCode() Method In the hashing algorithm, the hashCode method maps some keys with their values. The values are stored in certain memory locations using the available hash data structures such as HashMap, HashSet and HashTable.

What does the hashCode () method?

The hashCode method is an inbuilt method that returns the integer hashed value of the input value. Here are a few key concepts to remember: Multiple invocations of the hashCode must return the same integer value within the execution of a program unless the Object used within the equals method changes.

What is the return type of hashCode () method in the object class?

hashCode() Method The hashCode() is a method of Java Integer Class which determines the hash code for a given Integer. It overrides hashCode in class Object. By default, this method returns a random integer that is unique for each instance.


2 Answers

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)

like image 117
nkukhar Avatar answered Sep 19 '22 07:09

nkukhar


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
like image 32
Peter Lawrey Avatar answered Sep 21 '22 07:09

Peter Lawrey