Home » Java programming language

Java System class identityHashCode() method with example

System class identityHashCode() method: Here, we are going to learn about the identityHashCode() method of System class with its syntax and example.
Submitted by Preeti Jain, on September 14, 2019

System class identityHashCode() method

  • identityHashCode() method is available in java.lang package.
  • identityHashCode() method is used to return the hashcode of the given object – By using this method the value of hashcode will be the same as the value of hashcode by using hashCode() method.
  • Let suppose, if we pass an object that holds null value then in that case, the value of hashCode will be 0.
  • identityHashCode() method is a static method so this method is accessible with the class name too.
  • identityHashCode() method does not throw any exception.

Syntax:

    public static int identityHashCode(Object obj);

Parameter(s):

  • obj – represent the object for which the hashcode is to be returned.

Return value:

The return type of this method is int, it returns the hashcode of the given argument.

Example:

// Java program to demonstrate the example of 
// identityHashCode () method of System Class

import java.lang.*;
import java.io.*;

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

        File file1 = new File("Java");
        File file2 = new File("Programming");

        // getting hashcode
        int hcode1 = System.identityHashCode(file1);
        System.out.println(hcode1);

        // getting hashcode
        int hcode2 = System.identityHashCode(file2);
        System.out.println(hcode2);
        
    }
}

Output

E:\Programs>javac IdentityHashCodeMethod.java
E:\Programs>java IdentityHashCodeMethod
1018081122
242131142


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.