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

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

问题描述

如果执行:

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

您可能会看到输出


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() method 了解更多信息。

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

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

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