java.util.Objects.isNull vs object == null

Lucas T picture Lucas T · Jun 22, 2016 · Viewed 88.1k times · Source

As you know, java.util.Objects is

This class consists of static utility methods for operating on objects.

One of such methods is Objects.isNull().

My understanding is that Objects.isNull() would remove the chance of accidentally assigning a null value to object by omitting the second =.

However, the API Note states:

This method exists to be used as a Predicate, filter(Objects::isNull)

Would there be any reason/circumstance for which I should use object == null over Objects.isNull() in an if statement?

Should Objects.isNull() be confined to Predicates exclusively?

Answer

Suresh Atta picture Suresh Atta · Jun 22, 2016

should use object == null over Objects.isNull() in a if statement?

If you look at the source code of IsNull method,

 /* Returns true if the provided reference is null otherwise returns false.*/

 public static boolean isNull(Object obj) {
     return obj == null;
 }

It is the same. There is no difference. So you can use it safely.