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

标签 java hash-code-uniqueness

虽然两个不同的对象可能具有相同的哈希码,但是,System.identityHashCode() 似乎返回对象的内存指针。我想 32 位 JVM 实现也不异常(exception),包括 Sun JDK、Open JDK。不过我没有检查源代码。实际上,我可以假设具有相同 System.identityHashCode() 的两个对象是相同的吗?

最佳答案

答案是否定的。

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(). The hash code for the null reference is zero.

虽然对于 Object.hashCode()

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.)

但是,有一个bug在 Sun JDK 中表示两个对象可以返回相同的哈希码。

关于java - 我可以假设具有相同 System.identityHashCode 的两个对象是相同的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10645494/

相关文章:

algorithm - HashMap 中的简单哈希码误解?

c# - 复杂对象图的快速哈希码

java - 使用 Stax 通过 DTD 解析 XML 时出错

java - 使用mysql数据库的SQL语句语法

java - 用Java实现单例数据库连接

java - 非 servlet 从 web.xml 获取属性

hash - 不同文件大小的哈希冲突与相同文件大小的哈希冲突的可能性相同吗?

java - 可以从 null 转换为 int 吗?

hash - 是否存在可以保证哈希算法唯一的情况?