Efficient and Elegant Null Checks in Java Using Utility Classes

This article explains how to perform efficient and elegant null checks in Java by first identifying the data type, then selecting the appropriate utility class such as StringUtils, ObjectUtils, Collections or CollectionUtils, and finally applying the class’s isEmpty method with code examples for strings, arrays, lists, maps, and more.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Efficient and Elegant Null Checks in Java Using Utility Classes

NullPointerException is a common bug; many developers add a !=null check to solve it.

Frequent use of !=null can be cumbersome; we can handle it efficiently and elegantly.

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

Step 2: Choose the corresponding utility class.

String → StringUtils

Object → ObjectUtils

Array → Arrays

List and Map → Collections, CollectionUtils

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

1. For String, use StringUtils.isEmpty:

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

The isEmpty method checks both null and empty string.

2. For Object, use ObjectUtils.isEmpty:

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

3. For Map, also use ObjectUtils.isEmpty:

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

4. For List, use ObjectUtils.isEmpty:

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

5. For arrays, ObjectUtils.isEmpty works as well:

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

ObjectUtils.isEmpty handles Optional, CharSequence, arrays, Collections, and Maps, covering most data types.

However, it only checks collection size, so a List with a single null element is considered non‑empty:

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

For such cases, iterate over elements, e.g. using Arrays.stream:

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

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

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

Similarly, CollectionUtils.isEmpty(list) checks both null and empty list.

Conclusion

Null checks can be performed in three steps: determine the data type, select the appropriate utility class, and apply its isEmpty method. Use StringUtils for strings, ObjectUtils for most other types, and CollectionUtils for collections when needed.

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.

JavaBackend DevelopmentStringUtilsnull checkObjectUtils
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.