In Java, System.identityHashCode(x) returns the hash code that Object.hashCode() would return for x, even if the class of x overrides hashCode(). It is based on the object's identity (which object it is), not on its contents.
Classes such as String override hashCode() so that equal values get equal codes. identityHashCode ignores that. It is used by java.util.IdentityHashMap, by debuggers, and in the default toString() text such as Object@1b6d3586.
A static method of the java.lang.System class, added in Java 1 (JDK 1.1). Call it on the class name, as in System.identityHashCode(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass any object, or null:
Java
int a = System.identityHashCode(obj); // stays the same for this object
int b = obj.hashCode(); // may be overridden by the class
int z = System.identityHashCode(null); // always 0
Call
What it does
System.identityHashCode(obj)
Identity-based hash of obj; the same for the object's whole lifetime.
System.identityHashCode(null)
Returns 0.
System.identityHashCode(o) == o.hashCode()
true when the class does not override hashCode().
new IdentityHashMap<>()
A map that compares keys with == and identity hash codes.
The actual number is chosen by the JVM and differs between runs and computers, so never store it, print it in tests, or use it as a unique ID.
Library declarations
Java
// java.lang.System
public static native int identityHashCode(Object x)
Parameters
Parameter
Description
x
The object to hash. May be null.
Return Value
Return Value
Description
int
The identity hash code of x, or 0 if x is null.
Compare with Similar Methods
Identity vs equality:
Method
How it differs
obj.hashCode()
Content-based for many classes (String, Integer, List); same values give same codes.
a == b
The real identity test: true only for the very same object.
a.equals(b)
Equality of contents, as defined by the class.
Objects.hashCode(obj)
obj.hashCode(), or 0 for null; not identity-based.
Java Examples
Example 1 — Stable Codes and the null Case
The identity hash code of one object never changes. The raw numbers vary per run, so only comparisons are printed, plus the fixed value for null.
Java
public class SystemIdentityHashCodeSame {
public static void main(String[] args) {
Object box = new Object();
int first = System.identityHashCode(box);
int second = System.identityHashCode(box);
System.out.println("Same object, same code: " + (first == second));
System.out.println("Equals Object.hashCode(): " + (box.hashCode() == first));
System.out.println("identityHashCode(null): " + System.identityHashCode(null));
}
}
Output
Same object, same code: true
Equals Object.hashCode(): true
identityHashCode(null): 0
How It Works
Ask twice. Both calls receive the same box, and an object's identity hash code is fixed for its lifetime, so the comparison is true.
No override, same number.Object does not override hashCode(), so box.hashCode() is the identity hash code too.
null is defined. The documentation says identityHashCode(null) is 0, so that one value is safe to print; calling null.hashCode() would throw instead.
Example 2 — Equal Strings vs the Same String Object
String overrides hashCode() with a formula based on its characters. Identity is a different question, answered by ==.
Java
public class SystemIdentityHashCodeStrings {
public static void main(String[] args) {
String a = new String("hello");
String b = new String("hello");
System.out.println("a.equals(b): " + a.equals(b));
System.out.println("a == b: " + (a == b));
System.out.println("Same hashCode(): " + (a.hashCode() == b.hashCode()));
System.out.println("hashCode() of hello: " + a.hashCode());
String c = "hello";
String d = "hello";
System.out.println("Two literals, one object: " + (c == d));
System.out.println("So identity codes match: " + (System.identityHashCode(c) == System.identityHashCode(d)));
}
}
Output
a.equals(b): true
a == b: false
Same hashCode(): true
hashCode() of hello: 99162322
Two literals, one object: true
So identity codes match: true
How It Works
Two objects, equal text.new String("hello") always creates a new object, so a == b is false even though equals is true.
A content hash.String.hashCode() is computed from the characters (s[0]*31^(n-1) + ... + s[n-1]), so it is 99162322 for "hello" on every JVM; their identity hash codes would usually differ, but that is not guaranteed and is not printed.
Interned literals. Identical string literals refer to one shared object, so c == d is true, and the same object must have the same identity hash code.
Example 3 — Count Objects by Identity with IdentityHashMap
IdentityHashMap uses identityHashCode and == for its keys, so two equal but separate keys are stored twice.
Java
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
public class SystemIdentityHashCodeMap {
public static void main(String[] args) {
String k1 = new String("id-7");
String k2 = new String("id-7");
Map<String, String> byValue = new HashMap<>();
byValue.put(k1, "first");
byValue.put(k2, "second");
Map<String, String> byIdentity = new IdentityHashMap<>();
byIdentity.put(k1, "first");
byIdentity.put(k2, "second");
System.out.println("HashMap size: " + byValue.size());
System.out.println("IdentityHashMap size: " + byIdentity.size());
System.out.println("HashMap value for k1: " + byValue.get(k1));
System.out.println("IdentityHashMap value for k1: " + byIdentity.get(k1));
}
}
Output
HashMap size: 1
IdentityHashMap size: 2
HashMap value for k1: second
IdentityHashMap value for k1: first
How It Works
HashMap compares contents.k1.equals(k2) is true, so the second put replaces the value of the existing entry and the size stays 1.
IdentityHashMap compares objects. It hashes keys with System.identityHashCode and matches them with ==, so k1 and k2 are two different keys.
Lookups follow the same rule.byValue.get(k1) finds the overwritten value "second", while byIdentity.get(k1) finds the entry stored under that exact object, "first".
Frequently Asked Questions
The hash code Object.hashCode() would return for the object, based on its identity, even if its class overrides hashCode(). For null it returns 0.
hashCode() can be overridden, and String or Integer base it on their contents. identityHashCode() always uses the default identity-based value.
No. Different objects usually get different values, but collisions are possible. Use == to check whether two references point to the same object.
No. It stays the same for the lifetime of the object in one run, even if its fields change or the garbage collector moves it.
In IdentityHashMap, in code that tracks objects already seen while copying a graph, in debuggers, and in the default Object.toString() output.