Java null check why use == instead of .equals ()

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

+110
java null
Dec 21 '10 at 15:47
source share
13 answers

These are two completely different things. == compares the reference to the object, if any, contained in the variable. .equals() checks to see if two objects are consistent with their contract, which means equality. It is possible that two separate instances of the object will be β€œequal” in accordance with their contract. And then there are small details that with equals are a method, if you try to call it in a null reference, you will get a NullPointerException .

For example:

 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 
+167
Dec 21 '10 at 15:50
source share

if you call .equals() on null you will get a NullPointerException

Therefore, it is always recommended that you check the nullity value before calling the method when it is applied.

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

Also see

  • difference between equals and == in java
+34
Dec 21 2018-10-21
source share

In addition to the accepted answer ( https://stackoverflow.com/a/345947/ ):

Starting with Java 1.7, I recommend using

 Objects.equals(onePossibleNull, twoPossibleNull) 

to compare two objects that may be empty.

java.util.Objects

This class consists of static utility methods for working on objects. These utilities include null or nullable methods for computing the hash of an object, returning a string for the object and comparing two objects.

C: 1.7

+19
Jun 28 '17 at 10:21
source share

In Java 0 or null, there are simple types, not objects.

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

+17
Dec 21 '10 at 15:55
source share
 foo.equals(null) 

What happens if foo is null?

You get a NullPointerException.

+4
Dec 21 2018-10-21
source share

If the Object variable is NULL, you cannot call the equals () method on it, so the correct reference to the object is null.

+2
Dec 21 2018-10-21
source share

If you try to call the reference to the null object equal, then you will get an exception with a null pointer.

+2
Dec 21 '10 at 15:50
source share

According to the sources, it does not matter what to use to implement the default method:

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

But you cannot be sure of equals in a custom class.

+2
Dec 21 '10 at 15:53
source share

If we use the => .equals method

 if(obj.equals(null)) // Which mean null.equals(null) when obj will be null. 

When your object is empty, it will throw a Null Point exception.

therefore we must use ==

 if(obj == null) 

he will compare the links.

+2
02 Feb '16 at 9:25
source share

Since equal is a function derived from the Object class, this function compares the elements of the class. if you use it with a null value, it will return a false reason, because the class class of the class is not equal to zero. In addition, == compares the reference to the object.

+1
Dec 21 '10 at 15:51
source share

here is an example where str != null but str.equals(null) when using org.json

  JSONObject jsonObj = new JSONObject("{field :null}"); Object field = jsonObj.get("field"); System.out.println(field != null); // => true System.out.println( field.equals(null)); //=> true System.out.println( field.getClass()); // => org.json.JSONObject$Null 




EDIT: here is the class org.json.JSONObject$Null :

 /** * JSONObject.NULL is equivalent to the value that JavaScript calls null, * whilst Java null is equivalent to the value that JavaScript calls * undefined. */ private static final class Null { /** * A Null object is equal to the null value and to itself. * * @param object * An object to test for nullness. * @return true if the object parameter is the JSONObject.NULL object or * null. */ @Override public boolean equals(Object object) { return object == null || object == this; } } 
+1
Dec 10 '16 at 19:30
source share

So I never get confused and avoid problems with this solution:

 if(str.trim().length() <=0 ) { // is null ! } 
0
Oct 18 '17 at 12:24
source share

You can always do

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

This will first check the reference to the object, and then check the object itself by providing the link isnt null.

-3
Aug 16 2018-12-12T00:
source share



All Articles