如果 hashCode() 未被覆盖,对象的哈希码是什么? [英] What is an object's hash code if hashCode() is not overridden?

查看:20
本文介绍了如果 hashCode() 未被覆盖,对象的哈希码是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有覆盖 hashCode() 方法,那么在 Java 中对任何对象调用 hashCode() 的结果是什么?

If the hashCode() method is not overridden, what will be the result of invoking hashCode() on any object in Java?

推荐答案

通常情况下,如果不覆盖 hashCode() 只会返回对象在内存中的地址.

Typically, hashCode() just returns the object's address in memory if you don't override it.

来自 1:

在合理可行的情况下,类 Object 定义的 hashCode 方法确实为不同的对象返回不同的整数.(这通常是通过将对象的内部地址转换为整数来实现的,但是 JavaTM 编程语言不需要这种实现技术.)

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

这篇关于如果 hashCode() 未被覆盖,对象的哈希码是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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