JavaAntiPatterns

Collection of bad coding practices

‘equals()’ does not check for null argument

with 7 comments

If you override equals() method in your class, always check if an argument is null. If a null value is passed, equals() must unconditionally return false (no NullPointerException should be thrown!).

Bad:

class Person {
    ...
    public boolean equals(Object o) {
        return (o instanceof Person) && 
               this.getName().equals(((Person)o).getName());
    }
    ...
}

Good:

class Person {
    ...
    public boolean equals(Object o) {
        return (o != null) && 
               (o instanceof Person) && 
               this.getName().equals(((Person)o).getName());
    }
    ...
}
Advertisements

Written by Alex

November 22, 2007 at 2:08 pm

Posted in Objects

Tagged with , ,

7 Responses

Subscribe to comments with RSS.

  1. You don’t need o!=null comparison at all. In fact this is kind of premature optimization.

    According to JLS spec instanceof operator returns false when o is null. (See also Joshua Bloch, Effective Java). Also it is good to check whether o == this and skip some expensive comparisons.
    So the best form of equals may have following look:

    class Person {

    public boolean equals(Object o) {
    if (o == this)
    return true;
    if (!(o instanceof Person))
    return false;

    Person that = (Person)o;
    ….

    }

    Grzegorz Turski

    December 10, 2007 at 2:41 pm

  2. Thanks, Grzegorz!

    You’re right. I overlooked this instanceof convention. Have you any ideas also on the ‘bad code example’ ?

    Alex

    December 10, 2007 at 2:59 pm

  3. Not that I’m totally impressed, but this is a lot more than I expected when I stumpled upon a link on Furl telling that the info is quite decent. Thanks.

    How to Get Six Pack Fast

    April 15, 2009 at 3:17 pm

  4. Hi people, I just think this website is fantastic and I just cant wait for the new JLS Dolls to come out.
    My sister and I are going to buy everyone of the JLS dolls and have them in our school bags!
    We will be back soon
    Bye

    Gisela Chamber

    May 25, 2010 at 3:43 pm

  5. If you implement equals using instanceof, you should consider making equals and hashCode final. Otherwise, any subclass that overrides it must make certain to maintain the symmetry property: that subclass must be equal to the parent class. See http://www.artima.com/intv/bloch17.html

    David Phillips

    June 18, 2012 at 10:44 pm

  6. Heya are using WordPress for your blog platform?
    I’m new to the blog world but I’m trying to get started and create my own. Do you need any html coding knowledge to make your own blog? Any help would be really appreciated!

    lift butt dvd

    June 29, 2012 at 6:00 am

  7. Really needed ‘dislike’ button.
    just run: boolean articleIsIdiotizm = null instanceof Object;

    Petrov Andrei

    December 6, 2015 at 7:08 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: