Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java System identityHashCode() Method



Description

The Java System identityHashCode() method returns the same hash code for the given object as would be returned by the default method hashCode(). The hash code for the null reference is zero.

Declaration

Following is the declaration for java.lang.System.identityHashCode() method

public static int identityHashCode(Object x)

Parameters

x − This is the object for which the hashCode is to be calculated.

Return Value

This method returns the hashCode.

Exception

NA

Example: Getting hashcode for a File

The following example shows the usage of Java System identityHashCode() method. We've created three File objects and using identityHashCode() method, their hashcode is retrieved and printed.

package com.tutorialspoint;

import java.io.File;

public class SystemDemo {

   public static void main(String[] args) throws Exception {

      File file1 = new File("amit");
      File file2 = new File("amit");
      File file3 = new File("ansh");

      // returns the HashCode
      int ret1 = System.identityHashCode(file1);
      System.out.println(ret1);

      // returns different HashCode for same filename
      int ret2 = System.identityHashCode(file2);
      System.out.println(ret2);

      // returns the HashCode    
      int ret3 = System.identityHashCode(file3);
      System.out.println(ret3);
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

1159190947
925858445
798154996

Example: Getting hashcode for a String

The following example shows the usage of Java System identityHashCode() method. We've created three Strings and using identityHashCode() method, their hashcode is retrieved and printed.

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) throws Exception {

      String name1 = new String("amit");
      String name2 = new String("amit");
      String name3 = new String("ansh");

      // returns the HashCode
      int ret1 = System.identityHashCode(name1);
      System.out.println(ret1);

      // returns different HashCode for same name2
      int ret2 = System.identityHashCode(name2);
      System.out.println(ret2);

      // returns the HashCode    
      int ret3 = System.identityHashCode(name3);
      System.out.println(ret3);
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

1159190947
925858445
798154996

Example: Getting hashcode for an Object

The following example shows the usage of Java System identityHashCode() method. We've created three Student Objects and using identityHashCode() method, their hashcode is retrieved and printed.

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) throws Exception {

      Student student1 = new Student(1, "Julie") ;
      Student student2 = new Student(1, "Julie") ;
      Student student3 = new Student(2, "Adam") ;

      // returns the HashCode
      int ret1 = System.identityHashCode(student1);
      System.out.println(ret1);

      // returns different HashCode for same student
      int ret2 = System.identityHashCode(student2);
      System.out.println(ret2);

      // returns the HashCode    
      int ret3 = System.identityHashCode(student3);
      System.out.println(ret3);
   }
} 
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

925858445
798154996
681842940
java_lang_system.htm
Advertisements