Java isNull method

In this post, we will about Objects class’s isNull method.

Objects’s isNull() method is used to check if object is null or not. java.util.Objects class was introduced in java 7.

Here is simple example for Object's isNull method.

Output:

Is str1 null: false
Is str2 null: true

Let’s look at source code Object’s isNull() method.

As you can see, it just checks if obj is null or not.

Advantage of isNull over obj==null

  1. In case, you are checking if boolean variable is null or not. You can make typo as below.

    You can use isNull() method to avoid this kind of typos.

  2. Object’s isNull() method can be used with Java 8 lambda expressions.

It is cleaner and easier to write.

than to write:

That’s all about isNull method in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *