Java: Check if String is Null, Empty or Blank

Introduction

In Java, there is a distinct difference between null, empty, and blank Strings.

  • An empty string is a String object with an assigned value, but its length is equal to zero.
  • A null string has no value at all.
  • A blank String contains only whitespaces, are is neither empty nor null, since it does have an assigned value, and isn't of 0 length.
String nullString = null;
String emptyString = "";
String blankString = " ";

In this tutorial, we'll look at how to check if a String is Null, Empty or Blank in Java.

Using the Length of the String

As mentioned before, a string is empty if its length is equal to zero. We will be using the length() method, which returns the total number of characters in our string.

String blankString = " ";

if (blankString == null || blankString.length() == 0)
    System.out.println("This string is null or empty");
else
    System.out.println("This string is neither null nor empty");

The code above will produce the following output:

This string is null or empty

The String is blank, so it's obviously neither null nor empty. Now, based just on the length, we can't really differentiate between Strings that only contain whitespaces or any other character, since a whitespace is a Character.

Note: It's important to do the null-check first, since the short-circuit OR operator || will break immediately on the first true condition. If the string, in fact, is null, all other conditions before it will throw a NullPointerException.

Using the isEmpty() Method

The isEmpty() method returns true or false depending on whether or not our string contains any text. It's easily chainable with a string == null check, and can even differentiate between blank and empty strings:

String string = "Hello there";

if (string == null || string.isEmpty() || string.trim().isEmpty()) 
    System.out.println("String is null, empty or blank.");
else
    System.out.println("String is neither null, empty nor blank");

The trim() method removes all whitespaces to the left and right of a String, and returns the new sequence. If the String is blank, after removing all whitespaces, it'll be empty, so isEmpty() will return true.

Running this piece of code will give us the following output:

String is neither null, empty nor blank

Using the equals() Method

The equals() method compares the two given strings based on their content and returns true if they're equal or false if they are not:

String string = "Hello there";

if (string == null || string.equals("") || string.trim().equals(""))
    System.out.println("String is null, empty or blank");
else
    System.out.println("String is neither null, empty nor blank");

In much the same fashion as the before, if the trimmed string is "", it was either empty from the get-go, or was a blank string with 0..n whitespaces:

String is neither null, empty nor blank

Using the StringUtils Class

The Apache Commons is a popular Java library that provides further functionality. StringUtils is one of the classes that Apache Commons offers. This class contains methods used to work with Strings, similar to the java.lang.String.

If you're unfamiliar with Apache Commons' helper classes, we strongly suggest reading our Guide to the StringUtils class.

Since we'll be using Apache Commons for this approach, let's add it as a dependency:

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

Or, if you're using Gradle:

compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

One of the key differences between StingUtils and String methods is that all methods from the StringUtils class are null-safe. It additionally provides a few methods that we can leverage for this, including StringUtils.isEmpty() and StringUtils.isBlank():

String nullString = null;

if (nullString == null) {
    System.out.println("String is null");
}
else if (StringUtils.isEmpty(nullString)) {
    System.out.println("String is empty");
}
else if (StringUtils.isBlank(nullString)) {
    System.out.println("String is blank");
}

The output will be:

String is null

In addition to these, their inverse methods also exist: StringUtils.isNotEmpty() and StringUtils.isNotBlank(), though, you can achieve the same functionality by using the NOT (!) operator:

if (StringUtils.isNotEmpty(""))
    System.out.println("String is not empty");
    
// Equivalent to

if (!StringUtils.isEmpty(""))
    System.out.println("String is not empty");

Conclusion

A string is an object that represents a sequence of characters. Java provides many different methods for string manipulation. In this article, we have used some of these methods such as isEmpty(), equals(), StringUtils.isEmpty() and length() to check if the String is null, empty or blank.

Last Updated: February 28th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms