虽然两个不同的对象可能具有相同的哈希码,但是,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/