What is System identityHashCode() function in Java?

From the java.lang.* package, the identityHashCode() function returns the hash code regarding a defined object (argument value). This hash code is returned through the default function hashCode().

Syntax


static int identityHashCode(Object x)

Parameters

It takes an argument of Object type.

  • x: This represents the object for which the hash code will be calculated.

Return value

  • This method will return the hash code.
  • If the hash code is null, then a zero value will be returned as a reference.

Example

The code to learn about the working of identityHashCode() is as follows.

// Loaad necessary libraries
import java.lang.*;
import java.io.*;
public class EdPresso {
public static void main(String[] args) throws Exception {
// objects of File type
File f1 = new File("Ali");
File f2 = new File("Hamza");
File f3 = new File("Mohsin");
// returning the HashCode
int hashCode1 = System.identityHashCode(f1);
System.out.println(hashCode1);
// returning the different HashCode regarding similar filename
int hashCode2 = System.identityHashCode(f2);
System.out.println(hashCode2);
// returning the HashCode
int hashCode3 = System.identityHashCode(f3);
System.out.println(hashCode3);
}
}

Free Resources