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. Introduction

In this tutorial, we’ll discuss some ways of checking for empty or blank strings in Java. There are some native language approaches, as well as a couple of libraries.

2. Empty vs. Blank

Of course, it’s pretty common to know when a string is empty or blank, but let’s make sure we’re on the same page with our definitions.

We consider a string to be empty if it’s either null or a string without any length. If a string only consists of whitespace, then we call it blank.

For Java, whitespaces are characters, like spaces, tabs, and so on. We can check out Character.isWhitespace for examples.

3. Empty Strings

3.1. With Java 6 and Above

If we’re at least on Java 6, then the simplest way to check for an empty string is String#isEmpty:

boolean isEmptyString(String string) {
    return string.isEmpty();
}

To make it also null-safe, we need to add an extra check:

boolean isEmptyString(String string) {
    return string == null || string.isEmpty();
}

3.2. With Java 5 and Below

String#isEmpty was introduced with Java 6. For Java 5 and below, we can use String#length instead:

boolean isEmptyString(String string) {
    return string == null || string.length() == 0;
}

In fact, String#isEmpty is just a shortcut to String#length.

4. Blank Strings

Both String#isEmpty and String#length can be used to check for empty strings.

If we also want to detect blank strings, we can achieve this with the help of String#trim. It’ll remove all leading and trailing whitespaces before performing the check:

boolean isBlankString(String string) {
    return string == null || string.trim().isEmpty();
}

To be precise, String#trim will remove all leading and trailing characters with a Unicode code less than or equal to U+0020.

Also, we should remember that Strings are immutable, so calling trim won’t actually change the underlying string.

In addition to the above approach, as of Java 11, we can also use the isBlank() method instead of trimming:

boolean isBlankString(String string) {
    return string == null || string.isBlank();
}

The isBlank() method is a bit more efficient as well, as it doesn’t create a new String on the heap. As a result, if we’re on Java 11 or above, this is the preferred approach.

5. Bean Validation

Another way to check for blank strings is regular expressions. For instance, this comes in handy with Java Bean Validation:

@Pattern(regexp = "\\A(?!\\s*\\Z).+")
String someString;

The given regular expression ensures that empty or blank strings won’t validate.

6. With Apache Commons

If it’s ok to add dependencies, we can use Apache Commons Lang. This has a host of helpers for Java.

If we use Maven, we need to add the commons-lang3 dependency to our pom.xml:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>

Among other things, this gives us StringUtils.

This class comes with methods like isEmpty, isBlank, and so on:

StringUtils.isBlank(string)

This call does the same as our own isBlankString method. It’s null-safe and also checks for whitespaces.

7. With Guava

Another well-known library that brings certain string related utilities is Google’s Guava. Starting with version 23.1, there are two flavors of Guava: android and jre. The Android flavor targets Android and Java 7, whereas the JRE flavor goes for Java 8.

If we’re not targeting Android, we can just add the JRE flavor to our pom.xml:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>32.1.3-jre</version>
</dependency>

Guavas Strings class comes with the method Strings.isNullOrEmpty:

Strings.isNullOrEmpty(string)

It checks whether a given string is null or empty, but it won’t check for whitespace-only strings.

8. With Spring ObjectUtils

Furthermore, the Spring Core library provided a class named StringUtils which has a method to check if a string is empty. However, the class was deprecated in the Spring 5.3.0 version and above. It’s replaced with the ObjectUtils class.

To use the ObjectUtils class, let’s add the Spring Core dependency to our pom.xml:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.3.0</version>
</dependency>

The ObjectUtils class provides the isEmpty() method to check if a string is empty:

ObjectUtils.isEmpty(emptyString)

Like Guava, it won’t check if a string only contains whitespace but checks whether a given string is null or empty.

9. Conclusion

There are several ways to check whether a string is empty or not. Often, we also want to check if a string is blank, meaning that it consists of only whitespace characters.

The most convenient way is to use Apache Commons Lang, which provides helpers such as StringUtils.isBlank. If we want to stick to plain Java, we can use a combination of String#trim with either String#isEmpty or String#length. For Bean Validation, regular expressions can be used instead.

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