Master Elegant Null Checks in Java with StringUtils and ObjectUtils

This article explains how to replace repetitive !=null checks with efficient, type‑specific utility methods like StringUtils and ObjectUtils, offering a three‑step approach, code examples, and insights into their implementations for robust null handling in Java applications.

macrozheng
macrozheng
macrozheng
Master Elegant Null Checks in Java with StringUtils and ObjectUtils

Null pointer exceptions are common; instead of repeatedly writing != null checks, you can use utility classes to handle null checks efficiently.

Step 1

Identify the data type of the variable you need to check (e.g., String, Object, List, Array, Map, etc.).

Step 2

Select the appropriate utility class for the identified type:

String → StringUtils Object → ObjectUtils Array → Arrays List, Map → Collections or CollectionUtils These utilities are provided by the Java and Spring frameworks.

Step 3

Use the chosen utility class to perform the null or emptiness check.

String str = "";
StringUtils.isEmpty(str); // true
StringUtils

is specialized for String handling.

Object obj = null;
ObjectUtils.isEmpty(obj); // true
ObjectUtils

works for generic objects, maps, lists, and arrays.

Map<String, Object> map = Collections.emptyMap();
ObjectUtils.isEmpty(map); // true
List<Integer> list = Collections.EMPTY_LIST;
ObjectUtils.isEmpty(list); // true
Object[] arr = null;
ObjectUtils.isEmpty(arr); // true

The source code of ObjectUtils.isEmpty(Object obj) shows how it handles various types:

public static boolean isEmpty(@Nullable Object obj) {
    if (obj == null) {
        return true;
    }
    if (obj instanceof Optional) {
        return !((Optional) obj).isPresent();
    }
    if (obj instanceof CharSequence) {
        return ((CharSequence) obj).length() == 0;
    }
    if (obj.getClass().isArray()) {
        return Array.getLength(obj) == 0;
    }
    if (obj instanceof Collection) {
        return ((Collection) obj).isEmpty();
    }
    if (obj instanceof Map) {
        return ((Map) obj).isEmpty();
    }
    return false;
}

This method first checks for null, then determines the runtime type and applies the appropriate emptiness test. It covers Optional, CharSequence, arrays, collections, and maps.

However, for collections that contain elements, ObjectUtils.isEmpty only checks the collection size, not whether individual elements are null. For such cases you need to iterate the elements, e.g.:

Arrays.stream(list.toArray()).allMatch(ObjectUtils::isEmpty);

Similarly, for maps you can use CollectionUtils.isEmpty(map) which combines a null check and an isEmpty check.

Final Summary

To determine whether a value is null or empty in Java, follow three steps: (1) identify its data type, (2) choose the corresponding utility class, and (3) invoke the appropriate isEmpty method. Use StringUtils for strings, ObjectUtils for other types, and CollectionUtils for collections when needed. For element‑wise null checks in arrays or lists, iterate the elements manually.

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.

BackendStringUtilsnull checkObjectUtils
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.