Fundamentals 7 min read

Master Elegant Null Checks in Java with StringUtils and ObjectUtils

This guide explains how to efficiently and elegantly handle null pointer checks in Java by identifying data types, selecting the right utility classes such as StringUtils, ObjectUtils, Arrays, Collections, and applying their isEmpty methods to reduce repetitive code.

Open Source Linux
Open Source Linux
Open Source Linux
Master Elegant Null Checks in Java with StringUtils and ObjectUtils

Null pointer exceptions are common bugs; developers often add a != null check, but frequent checks can be cumbersome.

This article shows how to perform null checks efficiently and elegantly using appropriate utility classes.

Step 1 : Identify the data type (String, Object, List, Array, Map, etc.).

Step 2 : Choose the corresponding utility class: StringUtils for strings, ObjectUtils for objects, Arrays for arrays, Collections/CollectionUtils for collections.

Step 3 : Use the utility class to perform the check.

For String:

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

Implementation of StringUtils.isEmpty:

public static boolean isEmpty(@Nullable Object str) {
    return str == null || "".equals(str);
}

For Object, Map, List, and Array you can use ObjectUtils.isEmpty:

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

Map<String,Object> map = Collections.emptyMap();
ObjectUtils.isEmpty(map); // true

List<Integer> list = Collections.EMPTY_LIST;
ObjectUtils.isEmpty(list); // true

Object[] objArr = null;
ObjectUtils.isEmpty(objArr); // true

The ObjectUtils.isEmpty method handles many types by checking for null, Optional, CharSequence, arrays, Collections, and Maps.

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;
}

Note: ObjectUtils.isEmpty does not detect null elements inside collections or arrays. For such cases, iterate or use Streams:

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

For Map emptiness, CollectionUtils.isEmpty provides a concise check:

Map<String,Object> map = Collections.emptyMap();
CollectionUtils.isEmpty(map);

Similarly, CollectionUtils.isEmpty works for lists:

List<Integer> list = null;
CollectionUtils.isEmpty(list); // true

Summary : Determine the data type, select the appropriate utility class, and use its isEmpty method. Use StringUtils for strings; ObjectUtils for other types; and CollectionUtils for collections when needed. For nested null checks, iterate over elements.

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.

JavacodingStringUtilsnull checkObjectUtils
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.