Posts

Showing posts with the label comparing objects in java

Object Comparison: the equals and hashCode methods

Image
I wrote this back in March, 2012 for my Object-Oriented Programming class at Texas Wesleyan University in Fort Worth. I got this out of the "vault" because my son, who is in High School, has a friend who had a question on this very topic. I thought I had posted this to my blog and was shocked when I learned that was not the case. So, here we go... The equals method In my opinion, this is probably the most overlooked topic taught in Java. I have worked (and still work) with developers who seem to struggle with the concept of object comparison in Java. In Data Structures, you may have learned this concept. I will attempt to illustrate and simplify this concept using a "real life" example. For my illustration, I will use the famous Russian Dolls. A "Russian Doll," as it is typically known, is a set of wooden dolls of decreasing size placed one inside the other. Basically, all dolls with the exception of the smallest one are hollowed so t