为什么默认的Object.toString()返回hashCode的十六进制表示? [英] Why does the default Object.toString() return a hex representation of the hashCode?
问题描述
我很好奇为什么 Object.toString()
返回这个值:
return getClass()。getName()+@+ Integer.toHexString(hashCode());
与此相反:
<$ p $ (); getClass()。getName()+@+ hashCode();
将散列码显示为十六进制而不是十进制数可以带来什么好处吗?
Object.hashCode
用于计算基于对象所在的内存位置。内存位置几乎普遍显示为十六进制。
缺省返回值 toString
散列码,而是以调试的目的唯一标识对象的方式,并且散列码很好地用于识别目的(实际上,类名+存储器地址的组合是真正独特的;而散列码代码不保证是唯一的,它通常会接近)。
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?
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).
这篇关于为什么默认的Object.toString()返回hashCode的十六进制表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!