Register Login

Java String is NULL, Empty or Whitespace

Updated Jun 23, 2023

In the following sections of this Java article, we will discuss how to check whether a string is empty, null, or whitespace. This article will also highlight how to figure out the difference between these three, i.e., null string, empty string, and whitespace or blank strings.

Java strings are a sequence of characters in Java programming language as an object of the class java.lang.

Let us first see what null, empty, and whitespace or blank strings in Java are. The difference is simple, and we will specify using syntaxes:

  1. Users can specify an empty string as a string object where they can assign a value, but Java will represent the length as zero.
  2. Defining a null string can have no value.
  3. Users can specify only whitespaces using a blank string and neither empty nor null. Since this Java string contains an assigned value and does not have a zero length, it is also known as a whitespace character.
String a = null;
String b = "";
String c = " ";

The variable "a" has a null string, "b" has an empty string, and "c" has a blank Java string.

Following are the methods to check whether the Java string is empty, null, or whitespace. These are typical standard methods to check and return the value of the Java string:

  1. Using isNullOrEmpty("") method
  2. isEmpty() and isBlank("") method
  3. inEmpty method with any library
  4. String.length() method
  5. equals() method

Method 1: Using the Guava method or isNullOrEmpty("") method

Users can use this approach that leverages a convenient Google Guava library to check a Java string. It allows finding whether a string is not-null, not empty or has blank spaces.

Here, users can use the "standard Java" approach within the Guava approach to determine whether the string is empty or have whitespace. Add the following section of code in your Java program to check the string:

Code Snippet:

import com.google.common.base.Strings;
class Main
{
    public static void main(String[] args)
    {
        System.out.println(Strings.isNullOrEmpty(""));
        System.out.println(Strings.isNullOrEmpty("This is Guava approach")); 
        System.out.println(Strings.isNullOrEmpty(null));    
    }
}

Output:

Explanation:

In this example, we used the isNullOrEmpty(String) to check a null reference. It returns true when it finds a null reference, the string has a zero-length, or it contains only whitespaces.

Method 2: Using the Apache Commons Lang Approach or isEmpty() and isBlank("") method

Another standard approach to check Java string is using the Apache Commons Lang Approach, which uses StringUtils.isEmpty(String) static method. It allows users to check whether a string is empty, null or has whitespace.

Since users will use Apache Commons for this procedure, they must add the Apache library in their Java IDE as a dependency. Here is the code:

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

Add the following section of code in your Java program to check the string:

Code Snippet:

import org.apache.commons.lang3.StringUtils;
 
class Main
{
    public static void main(String[] args)
    {
        System.out.println(StringUtils.isEmpty(""));  
        System.out.println(StringUtils.isEmpty("This is an example of Apache Commons Lang Approach"));
        System.out.println(StringUtils.isEmpty(null));      
        System.out.println(StringUtils.isBlank(" "));   
    }
}

Explanation:

In this code snippet, we imported the Apache Commons Lang library, which uses these two StringUtils.isEmpty() and StringUtils.isBlank() static methods to specify if the Java string is not null, not empty, and not white space only.

Users can easily employ these methods, and the Apache Commons Lang approach is specifically concise. However, to use this library, your Java IDE must support this library as a standard part of the Java String.

Method 3: Using the inEmpty method with any library

It is one of the most recommended methods in Java from Java 7 onwards that allows users to check whether a string is empty, null, or has whitespace. It lets users avoid NullPointerException if users declare a null or blank string.

Code Snippet:

class Main
{
    public static void main(String[] args)
    {
        String a = "";
 
        System.out.println(a == null || a.isEmpty());   
    }
}

Output:

Explanation:

Here, we declared a string that has an empty string. Then we used the isEmpty() method with System.out.println() to print the result.

Code Snippet:

class Main
{
    public static void main(String[] args)
    {
        String a = " ";
 
        System.out.println(a == null || a.isEmpty());   
    }
}

Output:

Explanation:

But here, we have declared a blank string, and the method returned false because it only accepts an empty string and returns false in other cases.

Method 4: Using the String.length() method

This Java method returns the length of the specified string. By default, if users declare an empty string, the method will return zero as the length of the string.

Code Snippet:

class Demo
{
    public static void main(String[] args)
    {
        String str = null;
        System.out.println(str == null || str.length() == 0); 
    }
}

Output:

Explanation:

We used the length method to return whether the string is null or empty in the above code snippet. It will return the total number of characters in the Java string. But users must note that they should do the null-check first because the short-circuit "OR operator" (||) will evaluate directly on this first true condition.

And if users declare a null string, all other conditions before it will return a NullPointerException.

Method 5: Using the equals() method

This Java String class equals() method will compare the two initialized strings depending on the wt users give into the string. If it does not match any character, it returns false, and if matches all the characters, it returns true.

Code Snippet:

class Main
{
    private static String demo = "";
    public static void main(String[] args)
    {
        String a = "Hello";
        System.out.println(a == null || a.equals(demo));    
        System.out.println(a == null || demo.equals(a));    
    }
}

Output:

Explanation:

Users can use the String.equals() method, which executes an equality check against an empty string.

Conclusion:

As we have already discussed, a Java string is an object that specifies a sequence of characters. Java leverages several different methods for string evaluation.

We hope this article has given a crisp idea of how to test a string and the Java methods, such as isEmpty(), isBlank(), equals(), Guava approach, Apache Commons Lang Approach, and length() to check if the string is null, empty, or whitespace.


×