Though two different objects may have the same hash code, however, System.identityHashCode()
seems return the memory pointer of the object. I guess there could be no exception in 32-bit JVM implementations, includes Sun JDK, Open JDK. I didn't check the source code, though. In practice, can I assume two objects with the same System.identityHashCode()
are the same?
The short answer is no.
As per the documentation, System.identityHashCode(Object)
...
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().
So then lets check the documentation of Object.hashCode()
...
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java programming language.)
The answer is no.
System.identityHashCode() simply return the Object.hashCode().
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.
While for Object.hashCode()
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
However, there is a bug in Sun JDK indicated that two object can return same hashcode.
The short answer is no.
As per the documentation, System.identityHashCode(Object)
...
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().
So then lets check the documentation of Object.hashCode()
...
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java programming language.)
There are only two things in your question:
What does
System.identityHashCode(Object)
return.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.
What is the hashcode rule when comparing the equality of two objects.
Contract says, if two objects are equal using equals(object) method then they should have the same hashcode but if two objects have the same hashcode they are not necessarily equal.
© 2022 - 2024 — McMap. All rights reserved.
==
instead, which does mean they're the same. – Buddhism