Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the default Object.toString() return a hex representation of the hashCode?

I'm curious why Object.toString() returns this:

return getClass().getName() + "@" + Integer.toHexString(hashCode());

as opposed to this:

return getClass().getName() + "@" + hashCode();

What benefits does displaying the hash code as a hex rather than a decimal buy you?

like image 943
Tom Tresansky Avatar asked Sep 17 '10 14:09

Tom Tresansky


1 Answers

Object.hashCode used to be computed based on a memory location where the object is located. Memory locations are almost universally displayed as hexadecimal.

The default return value of toString isn’t so much interested in the hash code but rather in a way to uniquely identify the object for the purpose of debugging, and the hash code serve well for the purpose of identification (in fact, the combination of class name + memory address is truly unique; and while a hash code isn’t guaranteed to be unique, it often comes close).

like image 98
Konrad Rudolph Avatar answered Nov 15 '22 19:11

Konrad Rudolph