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

查看:32
本文介绍了为什么默认的 Object.toString() 返回 hashCode 的十六进制表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇为什么 Object.toString() 返回这个:

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

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

与此相反:

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

将哈希码显示为十六进制而不是十进制对您有什么好处?

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

推荐答案

Object.hashCode 用于计算基于对象所在的内存位置.内存位置几乎普遍显示为十六进制.

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

toString 的默认返回值不是对哈希码感兴趣,而是对唯一标识对象的方式感兴趣,以便调试,哈希码很好地用于识别的目的(实际上,类名+内存地址的组合确实是唯一的;虽然哈希码不能保证唯一,但它通常很接近).

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天全站免登陆