Course – LS (cat=Java)

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we’ll learn a few different ways to check if a given Integer instance’s value is null or zero.

For simplicity, we’re going to use unit test assertions to verify if each approach works as expected.

So, next, let’s see them in action.

2. Using the Standard Way

Using the logical OR operator could be the first idea to perform the check. It simply checks if the given Integer number is null or zero.

Let’s create a method to implement this check for easier verification:

public static boolean usingStandardWay(Integer num) {
    return num == null || num == 0;
}

This could be the most straightforward approach to performing the check. Let’s create a test to see if it works for different Integer instances:

int n0 = 0;
boolean result0 = usingStandardWay(n0);
assertTrue(result0);

boolean resultNull = usingStandardWay(null);
assertTrue(resultNull);

int n42 = 42;
boolean result42 = usingStandardWay(n42);
assertFalse(result42);

As we can see, we’ve tested our usingStandardWay() method by passing three Integer objects to it: zero, null, and 42. We’ll use these three Integer objects as inputs for further tests in this tutorial.

3. Using the Ternary Operator

We often use the ternary operator to write variable assignments with conditions, for example:

variable = booleanExpression ? expression1 : expression2

Next, let’s see how to use the ternary operator to perform the required Integer check:

public static boolean usingTernaryOperator(Integer num) {
    return 0 == (num == null ? 0 : num);
}

As the code above shows, (num == null ? 0 : num) first checks if the num variable is null. If this is the case, we take the zero. Otherwise, we would take the original num value.

In other words, this expression does a “null to zero” conversion here. As we’ve already handled the null case, we only need to check whether the result of the expression with the ternary operator is zero.

Next, let’s test if this works with our three Integer instances:

int n0 = 0;
boolean result0 = usingTernaryOperator(n0);
assertTrue(result0);

boolean resultNull = usingTernaryOperator(null);
assertTrue(resultNull);

int n42 = 42;
boolean result42 = usingTernaryOperator(n42);
assertFalse(result42);

The test passes if we give it a run. So, the usingTernaryOperator() method works as expected.

4. Using the Optional Class

Java 8 introduced the Optional type, so if our Java version is 8 or later, we can also implement this check using a static method from the Optional class:

public static boolean usingOptional(Integer num) {
    return Optional.ofNullable(num).orElse(0) == 0;
}

As we don’t know if the given Integer variable is null or not, we can build an Optional instance from it using Optional.ofNullable(num).

Next, we call Optional‘s orElse() method. The orElse(x) method checks the Optional object: if a value is present, it returns the value, otherwise, it returns x. Therefore, orElse(0) does the “null to zero” conversion as well. Then, the rest is pretty simple. We only need to check if orElse(0) is equal to zero.

Next, let’s verify if this approach works with our inputs:

int n0 = 0;
boolean result0 = usingOptional(n0);
assertTrue(result0);

boolean resultNull = usingOptional(null);
assertTrue(resultNull);

int n42 = 42;
boolean result42 = usingOptional(n42);
assertFalse(result42);

If we run the test, it passes. Therefore, the usingOptional() method does the job, too.

5. Using the ObjectUtils Class

Apache Commons Lang 3 is a pretty popular library. If this library is already a dependency of the Java project we’re working on, we can also perform the Integer check using its ObjectUtils class:

public static boolean usingObjectUtils(Integer num) {
    return ObjectUtils.defaultIfNull(num, 0) == 0;
}

As the implementation above shows, we called the ObjectUtils.defaultIfNull(num, 0) method in the check. The defaultIfNull() method is a generic method. If we take a look at how it’s implemented, we understand the usingObjectUtils() implementation:

public static <T> T defaultIfNull(final T object, final T defaultValue) {
    return object != null ? object : defaultValue;
}

As we can see, if we skip the generic declarations, defaultIfNull()‘s implementation is as simple as an expression using the ternary operator. This is not new to us. Therefore, it does the “null to zero” conversion, too. 

As always, let’s test our usingObjectUtils() method:

int n0 = 0;
boolean result0 = usingObjectUtils(n0);
assertTrue(result0);

boolean resultNull = usingObjectUtils(null);
assertTrue(resultNull);

int n42 = 42;
boolean result42 = usingObjectUtils(n42);
assertFalse(result42);

Unsurprisedly, the test passes as well.

6. Conclusion

In this article, we’ve explored three ways to check whether a given Integer object is null or its value is zero.

As usual, all code snippets presented in the article are available over on GitHub.

Course – LS (cat=Java)

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!