Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do hashCode() and identityHashCode() work at the back end?

How do Object.hashCode() and System.identityHashCode() work at the back end? Does identityHashCode() return the reference of the object? Does hashCode() depend on the content or address of the object?

What is the difference between hashCode() and identityHashCode()?

like image 839
Himanshu Avatar asked Feb 08 '11 08:02

Himanshu


People also ask

What is the difference between hashCode and identityHashCode in Java?

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.

How does the hashCode method work?

Simply put, hashCode() returns an integer value, generated by a hashing algorithm. Objects that are equal (according to their equals()) must return the same hash code. Different objects do not need to return different hash codes.

What is the return of the hashCode () method in the Object class?

The Java hashCode() Method hashCode in Java is a function that returns the hashcode value of an object on calling. It returns an integer or a 4 bytes value which is generated by the hashing algorithm.

What is identityHashCode in Java?

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.


1 Answers

How do Object.hashCode() and System.identityHashCode() work at the back end?

Assuming that it hasn't been overridden, the Object.hashCode() method simply calls System.identityHashCode(this).

The exact behavior of System.identityHashCode(Object) depends on the JVM implementation. (The actual implementation on recent Hotspot JVMs is rather clever, but I digress.)

Does identityHashCode() return the reference of the object?

No. It returns an int, and an int cannot hold a reference.

That integer returned by identityHashCode may be related to the (a) machine address for the object, or it may not be1. The value returned by identityHashCode() is guaranteed not to change for the lifetime of the object. This means that if the GC relocates an object (after an identityHashCode() call) then it cannot use the new object address as the identity hashcode.

Does hashCode() depend on the ? of the object ? == operator how to work in back end.

This doesn't make sense. There is no ? == or ?== operator in Java.

What is the difference between hashCode() and identityHashCode()?

This is partly explained above. Other differences include:

  • 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.

  • The identityHashCode(Object) method gives you a identifier for an object which can (in theory) be used for other things than hashing and hash tables. (Unfortunately, it is not a unique identifier, but it is guaranteed to never change for the lifetime of the object.)


1 - For current generation JVMs, it is not related to the memory address at all. See @bestsss's answer.

like image 121
Stephen C Avatar answered Sep 16 '22 19:09

Stephen C