5 ms·
Equality and comparisons in presence of subclassing etc. are not the business of a sane man. I think it is impossible to write an .equals() in java that satisfi
by jpr 17y ago
Equality and comparisons in presence of subclassing etc. are not the business of a sane man. I think it is impossible to write an .equals() in java that satisfies all the relevant laws people usually assume from it.
- joshhart 17y agoIn Eclipse: Alt-Shift-T for refactor. Select "Generate Hash-code and Equals".
- mhansen 17y agoequals(Object o) has a very tricky contract to implement, when you add subclassing to the mix. Auto-generated code won't make those problems go away. See http://www.artima.com/lejava/articles/equality.html http://www.artima.com/lejava/articles/equality.html
- joshhart 17y agoI was mostly joking when I wrote the parent comment. I've read Joshua Block's Effective Java which spends a long time discussing the challenges of properly implementing equals. Generally speaking, Eclipse gets it correct. Implementing equals properly boils down to following a set of rules - something that computers are exceptionally good at.
- jrockway 17y agoI upmodded this for comedy value. This is really how Java programmers think. So what if the code is wrong in corner cases, it was auto-cut-n-pasted!
- sketerpot 17y agoIt's possible to be locally sane. For example, suppose you define an abstract Number class, and define subclasses for ComplexFloat and IntFraction numbers. You could define .equals() methods on each of those that only compare to the same type, and work correctly. You don't always have to deal with weird issues.