Java Utility Library

Java Objects - nonNull() Method



The java.util.Objects.nonNull() method returns true if the provided reference is non-null otherwise returns false.

Syntax

public static boolean nonNull(Object obj)

Parameters

obj Specify a reference to be checked against null.

Return Value

Returns true if the provided reference is non-null otherwise false

Exception

NA

Example:

In the example below, the java.util.Objects.nonNull() method is used to check whether a specified object is non-null or not.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating Objects
    Object obj1 = 50;
    Object obj2 = null;
    
    //checking obj1 for non-null
    System.out.print("Is obj1 non-null: ");
    System.out.println(Objects.nonNull(obj1));

    //checking obj2 for non-null
    System.out.print("Is obj2 non-null: ");
    System.out.println(Objects.nonNull(obj2));
  }
}

The output of the above code will be:

Is obj1 non-null: true
Is obj2 non-null: false

❮ Java.util - Objects