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 }
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.
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>
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
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 }
You can also use Java Util.
if (Objects.isNull(myObject)) { // Null or empty } if (Objects.nonNull(myObject)) { // Not null or empty }
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);
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 }
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) }
If the string is null or empty and you want to assign a default value to it;
String name = StringUtils.defaultIfBlank(inputName, "John Doe");
This allows us to assign a safe default value if the Strings are null, empty or space.
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 }
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:
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!