Check string == null or empty Java

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.commonsgroupId>
    <artifactId>commons-lang3artifactId>
    <version>3.11version>
dependency>

Or, if you're using Gradle:

compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'

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.

How do you check if a string is not null or empty in Java?

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.

How do I check if a string is empty or null?

You can use the IsNullOrWhiteSpace method to test whether a string is null , its value is String. Empty, or it consists only of white-space characters.

Is empty string == null?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .

Is empty and null check in Java?

We can check whether a particular String is empty or not, using isBlank() method of the StringUtils class. This method accepts an integer as a parameter and returns true if the given string is empty, or false if it is not.