Open In App

System.identityHashCode() Method in Java With Examples

The java.lang.System.identityHashCode() is the method which is used to return the same hash code for any given object that is returned by the default method hashCode(). Also, for every hash code with a null reference zero is returned.

Points to be Remembered:



public static int identityHashCode(Object x)

Parameters: The parameter x is of the type of the Hash and refers to the hashCode that is needed to be calculated.



Return Value: This method returns the hashCode.

Below programs illustrate the use of java.lang.System.identityHashCode() method.

Program 1:




// Java program to demonstrate working
// of java.lang.System.identityHashCode() method.
import java.lang.*;
import java.io.*;
  
public class SystemCode1 {
  
    public static void main(String[] args) throws Exception
    {
  
        File filename1 = new File("Welcome");
        File filename2 = new File("Welcome");
        File filename3 = new File("Geek");
        File filename4 = new File("World");
  
        // Returns the HashCode
        int returnvalue1 = System.identityHashCode(filename1);
        System.out.println(returnvalue1);
  
        // Returns different HashCode for same filename
        int returnvalue2 = System.identityHashCode(filename2);
        System.out.println(returnvalue2);
  
        // Returns the HashCode
        int returnvalue3 = System.identityHashCode(filename3);
        System.out.println(returnvalue3);
  
        // Returns the HashCode
        int returnvalue4 = System.identityHashCode(filename4);
        System.out.println(returnvalue4);
    }
}

Output:
589431969
1252169911
2101973421
685325104

Explanation:
In the above program, different hashcode or number are generated from an object even if they have the same name. Like here we can see first two terms are same i.e., “Welcome”, but we have two different values, which are

respectively for first and second Welcome

Program 2:




// Java program to demonstrate working
// of java.lang.System.identityHashCode() method.
import java.lang.*;
import java.io.*;
  
public class SystemCode2 {
  
    public static void main(String[] args) throws Exception
    {
  
        File filename1 = new File("10");
        File filename2 = new File("shyam");
        File filename3 = new File("s12");
        File filename4 = new File("s12");
  
        // Returns the HashCode
        int returnvalue1 = System.identityHashCode(filename1);
        System.out.println(returnvalue1);
  
        // Returns the HashCode
        int returnvalue2 = System.identityHashCode(filename2);
        System.out.println(returnvalue2);
  
        // Returns different HashCode for same filename
        int returnvalue3 = System.identityHashCode(filename3);
        System.out.println(returnvalue3);
  
        // Returns different HashCode for same filename
        int returnvalue4 = System.identityHashCode(filename4);
        System.out.println(returnvalue4);
    }
}

Output:
589431969
1252169911
2101973421
685325104

Article Tags :