DEV Community

Cover image for Simplify Null Checks in Java: Writing Clean Code with Apache Commons Lang 3
Tamer Ardal
Tamer Ardal

Posted on • Edited on

Simplify Null Checks in Java: Writing Clean Code with Apache Commons Lang 3

Cover Photo by Redaco on dribble

In Java, null checks are generally done using == or !=. In addition, if we want to do an empty check, our condition will look like the following.

if (myString != null || myString != ""){
  // Not null or empty
}

if(myList != null || myList.size() != 0){
  // Not null or empty
}

if (myObject != null) {
  // Not Null
}
Enter fullscreen mode Exit fullscreen mode

Such controls make your code repetitive and difficult to manage as it grows.

This is where the Apache Commons Lang 3 library comes in. We will examine 3 classes to make null or empty checks in objects and lists more reliable and more readable. First, let’s see how to add this library to our project.

How to Add Apache Commons Lang3 to Your Project

If you are using Maven, you can add the following dependency to your pom.xml file:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version> <!-- Check the version -->
</dependency>
Enter fullscreen mode Exit fullscreen mode

If you are using Gradle, you can add the following dependency to your build.gradle file:

implementation 'org.apache.commons:commons-lang3:3.17.0'  // Check the version

Enter fullscreen mode Exit fullscreen mode

ObjectUtils for General Null Checks

ObjectUtils has several useful methods that you can use to check objects and assign default values. Two of them are the isEmpty and isNotEmpty methods. These methods check whether the object is null or empty.

if (ObjectUtils.isEmpty(myObject)) {
  // Null or empty
}

if (ObjectUtils.isNotEmpty(myObject)) {
  // Not null or empty
}
Enter fullscreen mode Exit fullscreen mode

You can also use Java Util.

if (Objects.isNull(myObject)) {
  // Null or empty
}

if (Objects.nonNull(myObject)) {
  // Not null or empty
}
Enter fullscreen mode Exit fullscreen mode

If you want to assign a default value to an object in case it is null, you can use the defaultIfNull method of the ObjectUtils class.

Integer age = ObjectUtils.defaultIfNull(inputAge, 18);

Enter fullscreen mode Exit fullscreen mode

Checking Strings for Null or Empty with StringUtils

Especially when working with String values, we need to do a lot of null or empty checking. In this case, we can easily do this with the StringUtils class.

if (StringUtils.isBlank(myString)) {
  // String is null, empty or contains only spaces
}

if (StringUtils.isNotBlank(myString)) {
  // String contains a valid value
}
Enter fullscreen mode Exit fullscreen mode

If you only want to check for null or empty;

if (StringUtils.isEmpty(myString)) {
  // String null or empty
}

if (StringUtils.isNotEmpty(myString)) {
  // String contains a valid value (Can contain only spaces)
}
Enter fullscreen mode Exit fullscreen mode

If the string is null or empty and you want to assign a default value to it;

String name = StringUtils.defaultIfBlank(inputName, "John Doe");

Enter fullscreen mode Exit fullscreen mode

This allows us to assign a safe default value if the Strings are null, empty or space.

Collection Checks with CollectionUtils

When working with collections, it is essential to check for lists that are empty. With the CollectionUtils class you can make such checks quite simple.

To check if a collection is empty;

if (CollectionUtils.isEmpty(myList)) {
    // List is empty
}

if (CollectionUtils.isNotEmpty(myList)) {
    // List is valid
}
Enter fullscreen mode Exit fullscreen mode

In this way, we can do both checks at once and make the code cleaner.

With Apache Commons Lang 3, you can make your code cleaner, reliable and maintainable by performing null checks in Java more easily. With this library, which is very easy to include in the project, you can reduce code complexity in your Java projects and create a better quality software development process by minimizing errors.

...

Thank you for reading my article! If you have any questions, feedback or thoughts you would like to share, I would love to hear them in the comments.

Thank you! 👨‍💻🚀

To follow me on LinkedIn: https://www.linkedin.com/in/tamerardal/
Medium: Simplify Null Checks in Java: Writing Clean Code with Apache Commons Lang 3

Resources:

  1. Educative IO, What is ObjectsNonNull in Java
  2. Apache Commons, StringUtils
  3. Apache Commons, Dependency Info
  4. Baeldung, Java Commons Lang 3

Top comments (1)

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise

I would say for those simple parts you can use, as already mentioned Objects.require.., also String.isBlank() etc. in comparison using a supplemental library...