Close

Java - System.identityHashCode() Examples

Java Java API 


Class:

java.lang.System

java.lang.Objectjava.lang.Objectjava.lang.Systemjava.lang.SystemLogicBig

Method:

public static int identityHashCode(Object x)

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode().


Examples


package com.logicbig.example.system;

public class IdentityHashCodeExample {

public static void main(String... args) {
String str = "testString";
System.out.println(System.identityHashCode(str));
}
}

Output

1246903245




package com.logicbig.example.system;

public class IdentityHashCodeExample2 {

public static void main(String... args) {
MyClass myClass = new MyClass(20);
int i = myClass.hashCode();
System.out.println(i);

int i2 = System.identityHashCode(myClass);
System.out.println(i2);
}

private static class MyClass {
private int i = 0;

public MyClass(int i) {
this.i = i;
}

@Override
public int hashCode() {
return i;
}
}
}

Output

20
148196010




See Also