为什么默认的Object.toString()返回hashCode的十六进制表示? [英] Why does the default Object.toString() return a hex representation of the hashCode?

查看:437
本文介绍了为什么默认的Object.toString()返回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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆