Check if Object Is Null in Java

[ad_1]

An object in Java is an instance of a class. It is a real entity existing in the memory opposite to the class that acts as a blueprint for the object.

The object represents data and methods for a particular entity that are defined by the class.

In this article, you will learn to check if the object contains a null reference in Java.

Comparison Operator to Check if Object Is Null in Java

The comparison operator (==) in Java is widely used to perform the comparison between two entities. The result of the comparison is boolean true if both the entities are the same, otherwise, the result is boolean false.

You can use this operator to check if an object is null in Java by comparing the object with the ‘null’ value.

The code defines a class named MyClass that has a String field name. There are two constructors to instantiate the object of the class.

The code defines two objects of the MyClass class named as myObj and myObj2. The myObj object is not instantiated therefore it should have the null reference. The other object is instantiated making it a non-null object.

Let us see the example using the code given below.

Output:

The first object is null
The second object is not null

isNull() Method to Check if Object Is Null in Java

The Objects class of Java’s Utility Library has a static method named isNull() to check if the object is null.

When you pass a reference of an object to the isNull() method, it returns a boolean value. It returns true if the reference is null, otherwise, it returns false.

The definition of the isNull() method is given below.

Let us see the example in code.

Output:

The first object is null
The second object is not null

If you want to know more about isNull() method, visit our article here.

nonNull() Method to Check if Object Is Null in Java

The nonNull() method is opposite to the isNull() method. It is also a static method defined in the Objects class.

When you pass a reference of an object to the nonNull() method, it returns a boolean value. It returns true of the reference is not null, otherwise, it returns false.

The definition of the nonNull() method is given below.


You can check if the object is null using this method by simply reversing the value returned by the method by putting negation operator (!) before the method calling statement.

So if the object is null, the method returns false. We reverse the ‘false’ to the ‘true’ so that the condition becomes true if the object is null.

Let us see the code.

Output:

The first object is null
The second object is not null

requireNonNull() Method to Check if Object Is Null in Java

The requireNonNull() method is also a static method of the Objects class. This method accepts an object reference and throws a NullPointerException if the reference is null.

You can use this method to check the parameters as well for other uses. The definition of the requireNonNull() method is given below.

To check if the object is null,

  • Invoke the method inside a try block by passing the reference of the object.
    • If the object is not null, the control will move to the next line.
    • Display the message of non-null object in the next line.
  • In the catch block, catch the NullPointerException
    • The control will move to the catch block only if the object is null and the exception is thrown.
    • Display the message of the null object inside the catch block.

Let us see the code.


Output:

The first object is null
The second object is not null

Conclusion

While working with the objects, it is important to check if the object is null so that you can avoid unexpected results and failures. Most of the time, the NullPointerException is the result of not checking the null condition on the objects.

You can read about different polymorphic forms of the methods used in the article here.

That’s all about how to check if object is null in java.

Hope you have enjoyed reading the article. Stay tuned for more articles.
Happy Learning!

[ad_2]

Leave a Comment