简体   繁体   English

我可以假设具有相同 System.identityHashCode 的两个对象是相同的吗?

[英]Can I assume two objects with the same System.identityHashCode are the same?

Though two different objects may have the same hash code, however, System.identityHashCode() seems return the memory pointer of the object.尽管两个不同的对象可能具有相同的哈希码,但是System.identityHashCode()似乎返回了对象的内存指针。 I guess there could be no exception in 32-bit JVM implementations, includes Sun JDK, Open JDK.我想在 32 位 JVM 实现中也不例外,包括 Sun JDK、Open JDK。 I didn't check the source code, though.不过,我没有检查源代码。 In practice, can I assume two objects with the same System.identityHashCode() are the same?在实践中,我可以假设具有相同System.identityHashCode()两个对象是相同的吗?

The answer is no.答案是不。

System.identityHashCode() simply return the Object.hashCode() . System.identityHashCode()只返回Object.hashCode()

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode().为给定对象返回与默认方法 hashCode() 返回的相同的哈希码,无论给定对象的类是否覆盖 hashCode()。 The hash code for the null reference is zero.空引用的哈希码为零。

While for Object.hashCode()而对于Object.hashCode()

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.尽可能实用,类 Object 定义的 hashCode 方法确实为不同的对象返回不同的整数。 (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.) (这通常是通过将对象的内部地址转换为整数来实现的,但是 JavaTM 编程语言不需要这种实现技术。)

However , there is a bug in Sun JDK indicated that two object can return same hashcode.但是,Sun JDK 中有一个错误表明两个对象可以返回相同的哈希码。

The short answer is no.最简洁的答案是不。

As per the documentation , System.identityHashCode(Object) ...根据文档System.identityHashCode(Object) ...

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode().为给定对象返回与默认方法 hashCode() 返回的相同的哈希码,无论给定对象的类是否覆盖 hashCode()。

So then lets check the documentation of Object.hashCode() ...那么让我们检查一下Object.hashCode()文档......

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.尽可能实用,类 Object 定义的 hashCode 方法确实为不同的对象返回不同的整数。 (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 .) (这通常是通过将对象的内部地址转换为整数来实现的,但Java 编程语言不需要这种实现技术。)

There are only two things in your question:你的问题只有两件事:

  1. What does System.identityHashCode(Object) return. System.identityHashCode(Object)返回什么。

    Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode().为给定对象返回与默认方法 hashCode() 返回的相同的哈希码,无论给定对象的类是否覆盖 hashCode()。 The hash code for the null reference is zero.空引用的哈希码为零。

  2. What is the hashcode rule when comparing the equality of two objects.比较两个对象的相等性时的哈希码规则是什么。

    Contract says, if two objects are equal using equals(object) method then they should have the same hashcode but if two objects have the same hashcode they are not necessarily equal. Contract 说,如果两个对象使用 equals(object) 方法相等,那么它们应该具有相同的哈希码,但如果两个对象具有相同的哈希码,则它们不一定相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM