Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
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

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Jul 12, 2019 in Java by Aditya98 (1.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...