Home > Java > javaTutorial > body text

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

Mary-Kate Olsen
Release: 2024-10-19 06:07:30
Original
885 people have browsed it

Simplify Null Checks in Java: Writing Clean Code with Apache Commons Lang 3
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
}
Copy after login

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>
Copy after login

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

Copy after login

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
}
Copy after login

You can also use Java Util.

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

if (Objects.nonNull(myObject)) {
  // Not null or empty
}
Copy after login

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);

Copy after login

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
}
Copy after login

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)
}
Copy after login

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

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

Copy after login

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
}
Copy after login

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

The above is the detailed content of Simplify Null Checks in Java: Writing Clean Code with Apache Commons Lang 3. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template