http://stackoverflow.com/questions/4501061/java-null-check-why-use-instead-of-equals



In Java I am told that when doing a null check one should use == instead of .equals(). What are the reasons for this?

share|improve this question
3 
The easiest thing is to try null checking with equals() and see. When you try it will instantly be obvious – Goran Jovic Dec 21 '10 at 15:50
 
By the way, a google search with keywords "java null check" (without quotes) gave me as one of the top hitsthis thread, which has the same info as the answers here. –  Mitch Schwartz Dec 21 '10 at 15:53 
add comment

They're two completely different things. == compares the object reference, if any, contained by a variable. .equals() checks to see if two objects are equal according to their contract for what equality means. It's entirely possible for two distinct object instances to be "equal" according to their contract. And then there's the minor detail that since equals is a method, if you try to invoke it on a nullreference, you'll get a NullPointerException.

For instance:

class Foo {
    private int data;

    Foo(int d) {
        this.data = d;
    }

    @Override
    public boolean equals(Object other) {
        if (other == null || other.getClass() != this.getClass()) {
           return false;
        }
        return ((Foo)other).data == this.data;
    }

    /* In a real class, you'd override `hashCode` here as well */
}

Foo f1 = new Foo(5);
Foo f2 = new Foo(5);
System.out.println(f1 == f2);
// outputs false, they're distinct object instances

System.out.println(f1.equals(f2));
// outputs true, they're "equal" according to their definition

Foo f3 = null;
System.out.println(f3 == null);
// outputs true, `f3` doesn't have any object reference assigned to it

System.out.println(f3.equals(null));
// Throws a NullPointerException, you can't dereference `f3`, it doesn't refer to anything

System.out.println(f1.equals(f3));
// Outputs false, since `f1` is a valid instance but `f3` is null,
// so one of the first checks inside the `Foo#equals` method will
// disallow the equality because it sees that `other` == null
share|improve this answer
1 
You should add the case for f1.equals(f3) just to make it clear what will happen. –  Hiro2k Dec 21 '10 at 16:35 
1 
@Hiro2k: Sure, why not. Done. :-) –  T.J. Crowder Dec 21 '10 at 16:38
 
do you mean public int data? –  Xepoch Dec 22 '10 at 16:10
 
@Xepoch: No, I don't generally create public fields (although it doesn't really matter for this example either way). Why? –  T.J. Crowder Dec 22 '10 at 16:11
add comment

if you invoke .equals() on null you will get NullPointerException

So it is always advisble to check nullity before invoking method where ever it applies

if(str!=null && str.equals("hi")){
 //str contains hi
}  

Also See

share|improve this answer
4 
Your example is generally better written as if ("hi".equals(str)). –  ColinD Dec 21 '10 at 15:51 
2 
@user368186: the point isn't whether the equals method includes a null check. If your object reference is null, then the call someObject.equals(null) will raise a NullPointerException without ever entering the equals method. –  Dave Costa Dec 21 '10 at 15:51
1 
@ColinD Agree just demonstrating here –  Jigar Joshi Dec 21 '10 at 15:52
1 
It's always advisable to avoid nulls at all cost, so you don't need null checks at all ;). –  fwielstra Dec 28 '10 at 15:44
add comment

In Java 0 or null are simple types and not objects.

The method equals() is not built for simple types. Simple types can be matched with ==.

share|improve this answer
 
upvote for the actual answer that is most useful as opposed to the obvious "NullPointerException will be returned" herp derp answer. –  volk Aug 20 '12 at 3:50
add comment

You could always do

if (str == null || str.equals(null))

This will first check the object reference and then check the object itself providing the reference isnt null.

share|improve this answer
 
if (str == null || str.equals(null) || str.equals("")) –  Louis Morda Sep 21 '12 at 17:21 
 
i used your answer and added a check for an empty string! if im not mistaken, null and "" are not the same thing. –  Louis Morda Sep 21 '12 at 17:21 
add comment

If an Object variable is null, one cannot call an equals() method upon it, thus an object reference check of null is proper.

share|improve this answer
add comment
foo.equals(null)

What happens if foo is null?

You get a NullPointerException.

share|improve this answer
add comment

If you try calling equals on a null object reference, then you'll get a null pointer exception thrown.

share|improve this answer
add comment

Because equal is a function derived from Object class, this function compares items of the class. if you use it with null it will return false cause cause class content is not null. In addition == compares reference to an object.

share|improve this answer
add comment

According to sources it doesn't matter what to use for default method implementation:

public boolean equals(Object object) {
    return this == object;
}

But you can't be sure about equals in custom class.

share|improve this answer