Madhu Madhu - 1 year ago 51
Java Question

Why does the default Object.toString() include the hashcode?

If you execute:


you probably see the output like


My question : Is there any specific reason why hashCode number is displayed there?

Answer Source

The object hash code is the only standard identifier that might allow you to tell different arbitrary objects apart in Java. It's not necessarily unique, but equal objects normally have the same hash code.

The default toString() method shows the object class and its hash code so that you can hopefully tell different object instances apart. Since it is also used by default in error messages, this makes quite a bit of sense.

See the description of the hashCode() method for more information.