JAVA对象的hashCode()方法返回值是内存地址吗?

java源码对hashCode()方法的注释

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.
The general contract of hashCode is:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
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 Java™ programming language.)
Returns:
a hash code value for this object.

对上述注释的翻译

返回对象的哈希代码值。支持这种方法有利于哈希表,例如java.util.HashMap提供的哈希表。
hashCode的一般约定是:
只要在Java应用程序的执行过程中对同一对象调用hashCode方法不止一次,hashCode方法就必须始终返回同一个整数,前提是在对象的equals比较中使用的信息没有被修改。这个整数不需要从一个应用的一次执行到同一应用的另一次执行保持一致。
如果根据equals(Object)方法,两个对象相等,那么对这两个对象中的每一个调用hashCode方法都必须产生相同的整数结果。
根据equals(Object)方法,如果两个对象不相等,那么对这两个对象中的每一个调用hashCode方法都必须产生不同的整数结果。但是,程序员应该知道,为不相等的对象生成不同的整数结果可能会提高哈希表的性能。
尽可能实际地,由类对象定义的hashCode方法确实为不同的对象返回不同的整数。(这通常是通过将对象的内部地址转换成整数来实现的,但是Java编程语言并不要求这种实现技术。)
返回:
此对象的哈希代码值。

结论

上下文对照, typically翻译为代表性的、通常的,既然不是always(总是),那就意味着未必总是内部地址,而是大多数情况将对象的内部地址转换成整数。

结论:hashCode()的返回值大多数情况是将对象的内部地址转换成整数并返回,少数情况不是这样,也就代表着不是内存地址。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值