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:

  • By default, every class either implicitly or explicitly provides a hashCode() method
  • A hashcode is generally a number generated from any object which allows objects to be stored or retrieved very quickly in a Hashtable.
  • In Java, hashCode() by default is a native method, which means that the method has a modifier ‘native’, when it is implemented directly in the native code in the JVM.
  • Used to digest all the the data stored in an instance of the class into a single hash value i.e., a 32-bit signed integer.
  • Syntax :

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    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

    • 589431969
    • 1252169911

    respectively for first and second Welcome

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    589431969
    1252169911
    2101973421
    685325104
    


    My Personal Notes arrow_drop_up

    lets make it a lil simple

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.