SoFunction
Updated on 2025-04-24

Judgment of !=null in Java

Null pointer exceptions are bugs with relatively high frequency. When null pointers appear, many friends habitually add a !=null to judge, and this bug is solved.

When !=null judgments frequently appear in the code, we will have a headache. Can we make this judgment efficiently and gracefully?

The answer is of course OK.

first step, When we want to make the judgment of !=null, please pause and see what type of data we want to make the judgment?

Data types are nothing more than String strings, Object/custom objects, List collections, Array arrays, Map collections, etc.

Step 2, think about what tool classes are corresponding to this data type?

String type, the corresponding tool class is StringUtils.

Object object, the corresponding tool class is ObjectUtils.

Array array, the corresponding tool class is Arrays.

The tool classes corresponding to List collections and Map collections include Collections, CollectionUtils, etc.

These tool classes are all tool classes that come with Java and Spring frameworks.

Step 3, use the corresponding type of tool class to make judgments.

1. If this data is of String type, please use the StringUtils tool class.

String str = "";
(str);  // true

The StringUtils tool class is more targeted and is a tool class for String strings.

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

In the isEmpty method, there is a judgment that is null and a judgment that is equal to an empty string.

2. If this data is of Object type, please use the ObjectUtils tool class.

Object obj = null;
(obj); // true

3. If this data is of Map type, you can also use the ObjectUtils tool class.

Map<String,Object> map = ();
(map);// true

4. If this data is of List type, you can also use the ObjectUtils tool class.

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

5. If this data is of array type, you can still use the ObjectUtils tool class.

// ArrayObject[] objArr = null;
(objArr); // true

The isEmpty() method in ObjectUtils can determine whether the string, array, Map collection, and List collection are equal to null.

Why can this isEmpty method determine so many data types? Let’s take a look at its source code.

public static boolean isEmpty(@Nullable Object obj) {
    // Determine whether obj is null, if it is directly f    if (obj == null) {
      return true;
    }
    // Determine whether obj is a subclass of Optional    if (obj instanceof Optional) {
      // If so, call the isPresent method to determine whether it is null      return !((Optional) obj).isPresent();
    }
    // Determine whether obj is a subclass of CharSequence    if (obj instanceof CharSequence) {
      // If yes, get the length. When the length is equal to 0, it is considered that this obj is an empty string      return ((CharSequence) obj).length() == 0;
    }
    // Determine whether obj is an array    if (().isArray()) {
      // If the length of the array is equal to 0, the array is considered an empty array      return (obj) == 0;
    }
    // Determine whether obj is a subclass of the Collection collection    if (obj instanceof Collection) {
      // Use the isEmpty method of the Collection subclass to determine whether the collection is empty      return ((Collection) obj).isEmpty();
    }
    // Determine whether obj is a subclass of the Map interface    if (obj instanceof Map) {
      // If so, perform a strong rotation and use the isEmpty method of the subclass to determine whether the set is empty or not      return ((Map) obj).isEmpty();
    }

    // else
    return false;
}

In this static method, first judge whether the passed obj object is equal to null, and if so, return. If it is not null, locate the data type of obj and then judge based on the data type.

In this method, Optional, CharSequence, Array, Collection, Map data types are encapsulated, covering almost all data types.

Through this source code, we can also see that it has some flaws in its judgment of complex types of sets. In other words, it only judges the length of the set. If the length of the set is 0, it considers the set to be empty.

// Create a List collection with only one elementList&lt;Integer&gt; list = (null);
(list); // false

In the above code, we create a List collection with only one element and this element is null.

If it is an object array, there is an object in the array, but this object is null, the judgment will fail.

The judgment of object array in the ObjectUtils class is another isEmpty method.

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

Therefore, in these two cases, it is not appropriate for us to use the isEmpty method of ObjectUtils. We need to judge whether each element in the set or array is null.

6. Correct judgment on whether the elements in the List collection are empty

(()).allMatch(ObjectUtils::isEmptyisNull);

The Arrays tool class is used here. You need to convert the List collection into an array first, and then use the Arrays tool class to determine whether the elements in the array are null one by one.

7. Decision on whether the Map collection is empty or null

Map<String,Object> map = ();
(map);

The source code of isEmpty judgment method in the CollectionUtils tool class:

public static boolean isEmpty(@Nullable Map<?, ?> map) {
    return map == null || ();
}

Two judgments are aggregated in this method. We can reduce our workload by calling it directly.

In addition, there is an isEmpty method for List collections in the CollectionUtils tool class:

List&lt;Integer&gt; list = null;
// Use the CollectionUtils tool class to determine whether the list collection is empty(list); // true

The isEmpty source code for List collection:

public static boolean isEmpty(@Nullable Collection<?> collection) {
    return collection == null || ();
}

In this method, there are both null judgments and isEmpty judgments. Aggregating two judgments, we can also reduce our workload by calling them directly.

Finally, let's summarize

To determine whether a data is null, you can go through three steps. The first step is to think about what data type it belongs to, the second step is to select the correct tool class based on the data type, and the third step is to use the correct tool class to make judgments.

For String string type data, use the StringUtils tool class directly.

The ObjectUtils tool class can be used for data types other than String.

For List collections and Map collections, in addition to ObjectUtils, you can also use the CollectionUtils tool class to make judgments.

To judge whether the child elements in the array and List collection are null, it is necessary to traverse the judgment.

This is the end of this article about the judgment of !=null in Java. For more related content /qq_37844084/article/details/144914147, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!