Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW