Java Null Checks, List and String Validation, and Optional Usage Guide

This article explains common Java null‑checking techniques for objects, Lists, and Strings, compares different utility methods, and introduces the Optional class with its creation patterns and typical usage scenarios to help avoid NullPointerException in backend development.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Java Null Checks, List and String Validation, and Optional Usage Guide

1. Preface

In real projects many places require null‑checking; without it a NullPointerException may be thrown.

We previously mentioned exception handling; now we look at common null‑checking methods.

Typical ways to check an object for null include java.util.Objects.nonNull(obj), Hutool's ObjectUtil, or simply null != obj.

2. List Null‑Check

For a List, being non‑null is different from having a size greater than zero; a null list means the container does not exist, while size 0 means it is empty.

Some developers use list.isEmpty(), which internally throws NullPointerException if the list is null.

Therefore the common pattern is list != null && list.size() > 0 or using Hutool's CollUtil.isEmpty. The same applies to Set and Map.

3. String Null‑Check

Calling methods like equals(...) or length() on a null String also throws NullPointerException.

Common approaches: if (a == null || a.equals("")) { /* ... */ } or more efficient: if (a == null || a.length() == 0) { /* ... */ } Since Java 6 you can use a == null || a.isEmpty(), or Apache Commons StringUtils.isNotBlank(a) / StringUtils.isNotEmpty(a) with their respective semantics.

4. Optional

Java's Optional class is designed to avoid NullPointerException. Common factory methods include .empty(), .of(T), .ofNullable(T). Useful instance methods are isPresent(), ifPresent(...), orElse(...), orElseGet(...), orElseThrow(), map(...), flatMap(...), and get().

Example code showing Optional creation:

public final class Optional<T> {
    private static final Optional<?> EMPTY = new Optional<>();
    private final T value;
    private Optional() { this.value = null; }
    private Optional(T value) { this.value = Objects.requireNonNull(value); }
    public static <T> Optional<T> empty() {
        @SuppressWarnings("unchecked")
        Optional<T> t = (Optional<T>) EMPTY;
        return t;
    }
    public static <T> Optional<T> of(T value) { return new Optional<>(value); }
    public static <T> Optional<T> ofNullable(T value) { return value == null ? empty() : of(value); }
}

4.1 Optional Object Creation

See the code block above for the internal implementation of the Optional class.

4.2 Usage Scenarios

Scenario 1: In a service layer, query an object, then use Optional to handle possible null values.

Scenario 2: Combine Optional with functional programming to achieve the same logic in a single line.

5. Summary

Each method has its appropriate scenario; while fluent APIs like Optional can make code more elegant, they may reduce readability, so choose according to project needs.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Listoptionalbest-practicesnull check
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.