DEV Community

Avnish
Avnish

Posted on

Best way to check for null values in Java?

Here are some examples of checking for null values in Java along with detailed explanations:

  1. Using simple if statement:
String str = null;
if (str == null) {
    System.out.println("String is null");
} else {
    System.out.println("String is not null");
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • str is initialized to null.
  • The if statement checks if str is equal to null.
  • If str is null, it prints "String is null"; otherwise, it prints "String is not null".

Output:

String is null
Enter fullscreen mode Exit fullscreen mode
  1. Using Objects.isNull() method:
import java.util.Objects;

String str = null;
if (Objects.isNull(str)) {
    System.out.println("String is null");
} else {
    System.out.println("String is not null");
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • str is initialized to null.
  • Objects.isNull() method from the java.util.Objects class is used to check if str is null.
  • If str is null, it prints "String is null"; otherwise, it prints "String is not null".

Output:

String is null
Enter fullscreen mode Exit fullscreen mode
  1. Using ternary operator:
String str = null;
String result = (str == null) ? "String is null" : "String is not null";
System.out.println(result);
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • str is initialized to null.
  • The ternary operator ?: is used to check if str is null.
  • If str is null, it assigns "String is null" to the result; otherwise, it assigns "String is not null".

Output:

String is null
Enter fullscreen mode Exit fullscreen mode
  1. Using Objects.requireNonNull() method:
import java.util.Objects;

String str = null;
try {
    Objects.requireNonNull(str);
    System.out.println("String is not null");
} catch (NullPointerException e) {
    System.out.println("String is null");
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • str is initialized to null.
  • Objects.requireNonNull() method from the java.util.Objects class is used to check if str is null.
  • If str is null, it throws a NullPointerException, which is caught by the catch block, printing "String is null"; otherwise, it prints "String is not null".

Output:

String is null
Enter fullscreen mode Exit fullscreen mode
  1. Using StringUtils.isEmpty() method from Apache Commons Lang library:
import org.apache.commons.lang3.StringUtils;

String str = null;
if (StringUtils.isEmpty(str)) {
    System.out.println("String is null or empty");
} else {
    System.out.println("String is not null or empty");
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • str is initialized to null.
  • StringUtils.isEmpty() method from the org.apache.commons.lang3.StringUtils class is used to check if str is null or empty.
  • If str is null or empty, it prints "String is null or empty"; otherwise, it prints "String is not null or empty".

Output:

String is null or empty
Enter fullscreen mode Exit fullscreen mode

These are different ways to check for null values in Java, each providing different functionalities and suited for different scenarios.

Top comments (0)