Java - Difference between System.identityHashCode() and hashCode()

Both System.identityHashCode() and hashCode() are equivalent in that they return a unique value for an object. But there are differences.

System.identityHashCode()

System.identityHashCode() is a method that returns the unique hashcode of an object.

The hashcode for the object passed as an argument is returned as an int.

public static native int identityHashCode(Object x)

You can use it like this: In the example below, str1 and str2 have the same hashcode because they point to the same object. But since str3 is a different object, the hashcode is different.

String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");

System.out.println("str1 hashCode ? " + System.identityHashCode(str1));
System.out.println("str2 hashCode ? " + System.identityHashCode(str2));
System.out.println("str3 hashCode ? " + System.identityHashCode(str3));

Output:

str1 hashCode ? 1789447862
str2 hashCode ? 1789447862
str3 hashCode ? 38997010

You can also compare it to other objects like below.

File file1 = new File("Hello");
String str2 = "Hello";

System.out.println("file1 hashCode ? " + System.identityHashCode(file1));
System.out.println("str2 hashCode ? " + System.identityHashCode(str2));

Output:

file1 hashCode ? 1789447862
str2 hashCode ? 38997010

Return value for null is 0, like System.identityHashCode(null).

hashCode()

hashCode() is defined in the Object class, which is the parent class of all objects.

public native int hashCode();

And subclasses can override hashCode().

That`s why there are cases where different objects but the same hashcode, like this:

String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");

System.out.println("str1 hashCode ? " + str1.hashCode());
System.out.println("str2 hashCode ? " + str2.hashCode());
System.out.println("str3 hashCode ? " + str3.hashCode());

Output:

str1 hashCode ? 69609650
str2 hashCode ? 69609650
str3 hashCode ? 69609650

Since str1 and str2 are the same object, the hashcode can be the same. But since str2 and str3 are different objects, I expected the hashcode to be different, but they are the same.

The reason is that hashCode() is overridden as follows in the String class.

public int hashCode() {
    int h = hash;
    if (h == 0 && value.length > 0) {
        char val[] = value;

        for (int i = 0; i < value.length; i++) {
            h = 31 * h + val[i];
        }
        hash = h;
    }
    return h;
}

If you look at the code, you are creating a hashcode with value, a string of String. So even if the object is different, if the string is the same, the same hashcode is returned.

Clean up

Object`s hashCode() method does not have a unique value for each object because it can be overridden in subclasses. This method can be overridden to express that the properties of the objects are the same. For example, if the hashcode of a String is, it means that the string is the same even if the objects are different.

On the other hand, System.identityHashCode() cannot be overridden and returns a unique hashCode of the object. It is recommended to use this method if you want to compare the objects themselves.

Reference

codechachaCopyright ©2019 codechacha