Fundamentals 9 min read

Efficient Null Checks in Java Using Utility Classes

This article explains how to efficiently perform null checks in Java by selecting appropriate utility classes such as StringUtils, ObjectUtils, Collections, and CollectionUtils for different data types, and also includes a brief notice about a free programmer book giveaway.

Architecture Digest
Architecture Digest
Architecture Digest
Efficient Null Checks in Java Using Utility Classes

The article starts with a notice that users can obtain a free programmer book by replying with a keyword in the backend system, and then shifts to a technical discussion about handling null pointer exceptions, which are common bugs in Java code.

When a null pointer occurs, many developers simply add a != null check, but the article proposes a more systematic three‑step approach to perform null checks efficiently.

Step 1: Identify the data type of the variable you need to check. Typical types include String , custom objects, List , arrays, and Map .

Step 2: Choose the appropriate utility class for the identified type. For example:

String → StringUtils

Object, List, Map, Array → ObjectUtils

Array → Arrays

Collections → Collections or CollectionUtils

Step 3: Use the selected utility class to perform the null or emptiness check.

Examples:

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

The StringUtils.isEmpty method checks both null and empty string cases.

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

For Map and List you can also use ObjectUtils or CollectionUtils :

Map
map = Collections.emptyMap();
ObjectUtils.isEmpty(map); // true
List
list = Collections.EMPTY_LIST;
ObjectUtils.isEmpty(list); // true

The source code of ObjectUtils.isEmpty(Object obj) demonstrates 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;
}

While this method covers many cases, it has limitations: for collections containing a single null element, ObjectUtils.isEmpty returns false . In such scenarios, you need to iterate over the elements and check each one individually.

List
list = Collections.singletonList(null);
ObjectUtils.isEmpty(list); // false

For arrays of objects, a separate overload exists:

public static boolean isEmpty(@Nullable Object[] array) {
return array == null || array.length == 0;
}

When you need to verify that every element in a List is empty, you can use:

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

For Map checks, CollectionUtils.isEmpty(map) aggregates both null and isEmpty checks:

public static boolean isEmpty(@Nullable Map
map) {
return map == null || map.isEmpty();
}

Similarly, CollectionUtils.isEmpty(Collection collection) handles null and isEmpty for collections.

In summary, to determine whether a variable is null or empty in Java you should:

Identify its data type.

Select the appropriate utility class (StringUtils for strings, ObjectUtils for most other types, CollectionUtils for collections).

Invoke the corresponding isEmpty method.

For nested collections or arrays, an element‑wise check is required.

Finally, the article reminds readers that they can claim a free programmer book by replying with the keyword “5000” in the backend system.

JavaCollectionsStringUtilsUtility Classesnull-checkObjectUtils
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login 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.