Showing posts with label equals(). Show all posts
Showing posts with label equals(). Show all posts

Sunday, 26 June 2011

Bad Habits with the equals() Method

I have this really bad habit, it’s something I know I shouldn't do, so this blog is to formally document my failing in the hope that I will change. This bad habit concerns the Java if statement and its use with the equals() method. Like me, most people, when comparing a string variable with a string constant would probably write something straight forward like this:

Saturday, 12 February 2011

Implementing the equals() hashCode() Contract

The Java documentation for the Object.equals() method states that: "it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes." This is rather vague, is it necessary, or just a good idea to implement hashCode() when you override equals()?