Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (10.2k points)

When a class in Java doesn't override hashCode(), printing an instance of this class gives a nice unique number.

The Javadoc of Object says about hashCode():

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.

But when the class overrides hashCode(), how can I get its unique number?

1 Answer

0 votes
by (46k points)

System.identityHashCode(yourObject) will provide the 'original' hash code of yourObject as an integer. Uniqueness isn't fundamentally confirmed. The Sun JVM implementation will give you a value which is similar to the original memory address for this object, but that's an implementation feature and you shouldn't rely on it.

 re. memory addresses and rolling objects.

Related questions

Browse Categories