CKTIS / Java
System.identityHashCode — an object's identity hash code
System.identityHashCode returns an object's identity-based hash code, bypassing an overridden hashCode() method. It returns 0 for null. The result is not a unique identifier and does not replace reference comparison with ==.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
public static int identityHashCode(Object x)System.identityHashCode(Object x) is a static method returning an int: the hash code that the default Object.hashCode() implementation would return for the object. It does not invoke the object's class override of hashCode(). For null, it returns 0 rather than throwing an exception.
An ordinary x.hashCode() call uses the implementation selected for the object's actual class; that implementation may calculate a hash from its contents. System.identityHashCode(x) concerns the identity of a particular instance, not value equality defined by equals(). Two objects equal according to equals() need not have equal identity hash codes.
The result stays consistent for the same object within one execution, but need not be the same across executions. Different objects can receive the same result: collisions are allowed. Equal results therefore do not prove that references point to the same object; use == for that check. Do not treat the result as a unique identifier, a persistent key, or a guaranteed memory address.
In the example, item and alias refer to the same object. The overridden hashCode() deliberately throws AssertionError, making an accidental call visible. The program compares results instead of printing the unpredictable hash value; the expected output is true, true, and 0, each on its own line.
Examples
public class Main {
static final class Item {
@Override
public int hashCode() {
throw new AssertionError("hashCode override called");
}
}
public static void main(String[] args) {
Item item = new Item();
Item alias = item;
int hash = System.identityHashCode(item);
System.out.println(item == alias);
System.out.println(hash == System.identityHashCode(alias));
System.out.println(System.identityHashCode(null));
}
}Result
true true 0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.