• Share this article:

equals() and hashcode() oh my!

Tuesday, October 2, 2007 - 23:34 by Wayne Beaton

While communicating with a colleague today, I was reminded today about how painful explaining equals() and hashcode() to new Java developers can be. What’s particularly frustrating is that it’s not really a Java issue. I remember learning all about the ins and outs of equality and hash in Smalltalk, and then struggling to help others understand. It’s one of those classic computer-sciency problems that I used to use as a discussion point when interviewing potential co-op hires; I don’t recall ever finding a student who really understood it, but it helped me sort out the ones who could at least start solving a problem. When it comes down to it, understanding how best to implement these two methods is just plain hard.

Since this continues to be laboured about in numerous forums, I really don’t want to get into the specifics of how these methods should be implemented. For what it’s worth, I can’t remember the last time I actually implemented these methods. I’ve been building enterprise applications for many years now, and I can’t honestly remember actually needing these methods in my domain objects. That’s probably not an accurate reflection of reality; I probably just don’t remember implementing them.

I’m not sure what the point of this posting is. Maybe, it’s that—despite having lots of great infrastructure, frameworks, and APIs—there are just some fundamental concepts you really need to understand in order to do good stuff. Or maybe I’m just mumbling…