Java – under what conditions can two different objects have the same hashcode () value?

All I know is: –

"Int hashcode() returns the memory address of the object and the default hash value of the object“

If references X and Y represent two different objects, the expression (x.hashcode() = = y.hashcode()) is not always false

Therefore, I want to ask under what circumstances the hash values of two different objects are the same

Solution

You can override hashcode in a class You usually overwrite it and the rewritten equal sign, so if A. equals (b) is true, a. hashcode() = = B. hashcode() is true (even if (a = = b) is false)

However, even if a.equals (b) is false, a.hashcode() = = b.hashcode() may still be true

As you can see in the Javadoc of object class:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>