System.identityHashCode() in Java

System.identityHashCode() in Java | For every object in Java, JVM generates a unique integer value which is called hashcode. This hashcode is the integer representation of reference. Hashcode is a 32-bit unique integer number and is used to differentiate one object from another object and also used for differentiating one group of objects from other groups of objects.

We can this JVM generated reference-based hashcode value by using the hashCode() method of java.lang.Object class. But if we are overriding the hashCode() method then it returns the hashcode value based on its state. Now, to get JVM generated reference-based hashcode value we can use identityHashCode() of the System class. Method prototype:-

public static native int identityHashCode(Object x);

It is a static, native method given in System class to return reference-wise JVM generated hashcode of the given object. Example to demonstrate identityHashCode() method,

public class Test {
  public static void main(String[] args) {

    Test t1 = new Test();
    Test t2 = new Test();
    Test t3 = t1;
    
    System.out.println(System.identityHashCode(t1));
    System.out.println(System.identityHashCode(t2));
    System.out.println(System.identityHashCode(t3));
  }
}

Output:-

1143839598
110718392
1143839598

Using identityHashCode() method when hashCode() method is overridden in the class,

public class A {

  int i;

  A(int i) {
    this.i = i;
  }

  @Override
  public int hashCode() {
    return i;
  }
}
public class Test {
  public static void main(String[] args) {

    A a1 = new A(10);
    A a2 = new A(20);
    A a3 = new A(10);
    
    System.out.println(a1.hashCode());
    System.out.println(System.identityHashCode(a1));
    
    System.out.println(a2.hashCode());
    System.out.println(System.identityHashCode(a2));
    
    System.out.println(a3.hashCode());
    System.out.println(System.identityHashCode(a3));
  }
}

Output:-

10
110718392
20
425918570
10
2143192188

In “A” class hashCode() method is overridden to return the hashcode value based on the state of the object. Therefore we get the same hashcode value for the a1 and a3 objects returned by the hashCode() method. But identityHashCode() method returns JVM generated reference-based hashcode value. Here, the reference of all objects is unique therefore we get a unique hashcode value returned by identityHashCode() method.

Using identityHashCode() method on String,

public class Test {
  public static void main(String[] args) {

    String s1 = new String("Java");
    String s2 = new String("Java");
    String s3 = new String("Python");
    
    System.out.println(s1.hashCode());
    System.out.println(System.identityHashCode(s1));
    
    System.out.println(s2.hashCode());
    System.out.println(System.identityHashCode(s2));
    
    System.out.println(s3.hashCode());
    System.out.println(System.identityHashCode(s3));
  }
}

Output:-

2301506
914424520
2301506
110718392
-1889329924
425918570

In the String class, the hashCode() method is overridden to return the hashcode value based on its content but not based on the reference. Therefore we can use identityHashCode() method of the System class to get reference-based hashcode value.

identityHashCode() vs hashCode() in Java

1) The identityHashCode() method is defined in java.lang.System class whereas the hashCode() method is defined in java.lang.Object class.

2) The hashCode() method can be overridden but identityHashCode() can’t be overridden.

3) The identityHashCode() method is a static method therefore it can be called on both Object and primitive values. But the hashCode() method is an instance method therefore it can be called only on Object but not on primitive values.

public class Test {
  public static void main(String[] args) {
    int i = 10;
    System.out.println(System.identityHashCode(i));
    
    // Compile time error
    // System.out.println(i.hashCode()); 
  }
}

Output:-

1143839598

4) We can’t call hashCode() method on null, or null reference variable. Calling identityHashCode() on null, or null reference variable returns 0.

public class Test {
  public static void main(String[] args) {
    System.out.println(System.identityHashCode(null));
    // System.out.println(null.hashCode()); 
    // compile time error
    
    String str = null;
    
    System.out.println(System.identityHashCode(str));
    // System.out.println(str.hashCode()); 
    // Exception:- java.lang.NullPointerException
  }
}

Output:-

0
0

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *