Check if Object Is Null in Java

Check if Object is null Java

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!

Related Posts

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in Java In this post, we will see how to print multiple […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusion In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can be confused with […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusion If you have worked […]

  • Java projects for beginners
    30 April

    Top 20 Java Projects for Beginners

    This tutorial provides the top 20 java projects for beginners for practice. These projects will help you to learn java quickly and efficiently.

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.