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.
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); // trueThe StringUtils.isEmpty method checks both null and empty string cases.
Object obj = null;
ObjectUtils.isEmpty(obj); // trueFor 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); // trueThe 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); // falseFor 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.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.