为什么默认的 Object.toString() 包含哈希码? [英] Why does the default Object.toString() include the hashcode?

查看:39
本文介绍了为什么默认的 Object.toString() 包含哈希码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你执行:

System.out.println(someObj.toString());

你可能会看到类似的输出

you probably see the output like

someObjectClassname@hashcodenumber

someObjectClassname@hashcodenumber

我的问题:这里显示hashCode数字有什么具体原因吗?

My question : Is there any specific reason why hashCode number is displayed there?

推荐答案

对象哈希码是唯一的标准标识符,可以让您在 Java 中区分不同的任意对象.它不一定是唯一的,但相等的对象通常具有相同的哈希码.

The object hash code is the only standard identifier that might allow you to tell different arbitrary objects apart in Java. It's not necessarily unique, but equal objects normally have the same hash code.

默认的toString() 方法显示对象类及其哈希码,以便您可以区分不同的对象实例.因为它也默认用于错误消息中,所以这很有意义.

The default toString() method shows the object class and its hash code so that you can hopefully tell different object instances apart. Since it is also used by default in error messages, this makes quite a bit of sense.

查看hashCode() 方法了解更多信息.

See the description of the hashCode() method for more information.

这篇关于为什么默认的 Object.toString() 包含哈希码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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