Can I assume two objects with the same System.identityHashCode are the same?
Asked Answered
T

3

6

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?

Tellurion answered 18/5, 2012 at 1:7 Comment(3)
Why don't you compare them with == instead, which does mean they're the same.Buddhism
If you like writing non-portable code, the answer is "yes" :)Countrywoman
I would not base application logic on the internal implementation of a method, especially given the excellent Javadoc for Object.hashcode.Chromogen
B
6

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

Battik answered 18/5, 2012 at 1:20 Comment(0)
D
8

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.

Drought answered 18/5, 2012 at 1:22 Comment(0)
B
6

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

Battik answered 18/5, 2012 at 1:20 Comment(0)
S
1

There are only two things in your question:

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

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

Satirist answered 18/5, 2012 at 1:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.