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

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.

Leave a Comment