When to use System.identityhashcode() and hashcode() method?*
The hashcode() method is a non-final instance method, and should be overridden in any class where the equals(Object) is overridden. By contrast, identityHashCode(Object) is a static method and therefore cannot be overridden.
hashCode in Java helps the program to run faster. For example, comparing two objects by their hashcodes will give the result 20 times faster than comparing them using the equals() function. This is so because hash data structures like HashMaps, internally organize the elements in an array-based data structure.
System. identityHashCode() is the method which is used to return the same hash code for any given object that is returned by the default method hashCode(). Also, for every hash code with a null reference zero is returned.
According to the javadoc, the System.identityHashCode(Object o)
:
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(). The hash code for the null reference is zero.
So, at the first place, System.identityHashCode(nullReference)
will always give you 0
, instead of nullReference.hashCode()
which will obviously give you a NullPointerException
at Runtime.
Let's, however, consider the following class:
public class MysteriousOne {
@Override
public int hashCode() {
return 0xAAAABBBB;
}
//override equals() and so on...
}
The class overrides hashCode()
, which is perfectly fine, even though the hash code for every instance would be the same, which however is not fine, if you want to distinguish identities of several instances. Usually, you'd try the output of the .toString()
method (which by default gives the classname, followed by a @
followed by the hashCode()
output), for example, to find out the real identity of an object, but is in this case the output would be the same:
MysteriousOne first = new MysteriousOne();
MysteriousOne second = new MysteriousOne();
System.out.println("First: " + first);
System.out.println("Second: " + second);
The output would be:
First: MysteriousOne@aaaabbbb
Second: MysteriousOne@aaaabbbb
So, having such implementation of hashCode()
is impossible to distinguish between identities of several instances. Which is where System.identityHashCode()
is being handy.
If you do
System.out.println("First: " + System.identityHashCode(first));
System.out.println("Second: " + System.identityHashCode(second));
you'd get two different numbers for the different instances, even though the hashCode()
of their class implementation returns a constant (actually here the overridden implementation of hashCode()
will not be called at all, as per javadoc):
First: 366712642
Second: 1829164700
Further, you can even pass primitives to System.identityHashCode(Object o)
, as they will be boxed to their corresponding wrappers:
int i = 5;
System.out.println(System.identityHashCode(i));
More info:
Object#hashCode()
and System#identityHashCode()
work at the back end?If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With