Friday, July 10, 2009

How to Write an Equality Method in Java

Martin Odersky, Lex Spoon, and Bill Venners have written an article on How to Write an Equality Method in Java. Given the amount of traffic my post on the same topic has attracted, I thought I might take a look. Their solution is the best I have seen, and well worth a look, but the article suffers the same problem as every article I've read on this topic: it fails to properly define equals().

A Java equals() method should implement a test for bisimulation.

To me, the remarkable thing about the Artima article is that it provides and example where the traditional getClass() approach I have previously recommended fails to implement bisimulation. The good-news is that this is a rather artificial use-case, and as such isn't going to be causing many bugs. The bad-news is that the fix requires a helper method defined at the topmost concrete class, and the need for it isn't intuitive.

Anyway, if you program in Java you need to read and understand this article.

3 comments:

Patrick said...

Hey, you could at least have the decency to link the previous article.

Unknown said...

It's hard to tell with these Internet startups if they're really interested in building companies or if they're just interested in the money. I can tell you, though: If they don't really want to build a company, they won't luck into it. That's because it's so hard that if you don't have a passion, you'll give up.
Electrician Marbella

Unknown said...

very nice
hollywood actress