It seems that comparing two objects isn't as a simple task to do as it seems at first.

My concept for smart comparison still holds, but is completed perfectly with the How to Write an Equality Method in Java article posted on Artima soon after mine[1].

2 main points that you have to be careful to (Others are also detailed in this article) :

  • hasCode() has to be redefined usually since otherwise the equality contract on Object.hascode() would be broken.
  • the equals() has to take an Object as parameter since overloading in Java is resolved by the compile-time type of the argument, not the run-time type.

I just got bitten by the second one, but not much (yet) since I mostly rely on compile-time overloading. My purpose here was only to compare objects either to constants or to simple variables. But in the generalization I would certainly have overlooked this and be bitten much more deeply.


[1] But as much as I wished it to be, it is not related :-)