Always override
Quotes:
http://www-106.ibm.com/developerworks/library/j-jtp06294.html
Hashing it out
In " Hashing it out," I outlined the basic rules for correctly overriding
Object.equals() and Object.hashCode() in the same time. I had met with such bugs serveral times and it was a hard time for me to figure out such bug.Quotes:
http://www-106.ibm.com/developerworks/library/j-jtp06294.html
Hashing it out
In " Hashing it out," I outlined the basic rules for correctly overriding
Object.equals() and
Object.hashCode(), in particular that equal objects (according to
equals()) must have equal
hashCode() values. While this rule is fairly easy to follow once you know it (and some IDEs have wizards for defining both for you in a consistent manner), if you override one of these methods and forget to override the other, this bug can be very difficult to detect through inspection -- because the error is not in the code that is present, but in the code that is absent.
博客强调要同时重写相关方法,作者曾多次遇到此类未同时重写导致的bug,排查困难。还提到正确重写方法的基本规则,即相等对象必须有相等的哈希值,若只重写其一,错误难通过检查发现。

5万+

被折叠的 条评论
为什么被折叠?



