Метод System.identityHashCode () в Java с примерами

Опубликовано: 13 Февраля, 2022

Java.lang.System.identityHashCode () - это метод, который используется для возврата одного и того же хэш-кода для любого заданного объекта, который возвращается методом по умолчанию hashCode (). Кроме того, для каждого хэш-кода с нулевой ссылкой возвращается ноль.

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 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:

    // 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

    • 589431969
    • 1252169911

    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
    

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.