views:

69

answers:

1

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?

+6  A: 

Object.hashCode by default returns the 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 than rather a way to uniquely identify the object for the purpose of debugging, and memory addresses serve well for the purpose of identification (in fact, the combination of class name + memory address is truly unique).

Konrad Rudolph
@Konrad - strictly speaking `Object.hashCode()`, it returns a number that *for some JVMs* is the based on the location of the object *at the time when the method is first called*. The GC may relocate the object, but the `hashCode` must remain the same.
Stephen C