Java – Check if a String is Empty or Blank

In Java, empty String and blank String are two different concepts. It’s always confusing and tricky for Java beginners to check if a String is both, empty or blank. An empty String is a String object initialized without any character where as a blank string is a String with a whitespace characters. Always remember that a String with just whitespace may not be considered as an empty String by one program but considered as an empty String by others. Depending upon your situation, you can include the logic to check for that as well. A String with just white space is also referred to as a blank String in java. In this tutorial, I will teach you a couple of right ways to check if a String is empty or blank in Java.

Check if String is null or empty in Java

To 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 only, then we call it blank.

For Java, whitespaces are characters like spaces, tabs and so on. Have a look at Character.isWhitespace for examples.

Below are the different ways to check if String is empty or blank in Java.

1) Empty String check with Java 6 and Above – Using isEmpty()

This is the most readable way to check for both whether String is empty or not. You can see from the below code that the first check is a null check and the second check is for emptiness.

/****************************************************************************************
 * Created on 06-2020 Copyright(c) https://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.java.lang;

/**
 * Created by https://kodehelp.com
 * Date: 06/22/2020
 */
public class StringNullEmptyCheck {
    public static void main(String[] args) {
        String string1 = null;
        String string2 = "";

        if(isStringEmpty(string1)){
            System.out.println("string1 is Empty!");
        }

        if(isStringEmpty(string2)){
            System.out.println("string2 is Empty!");
        }
    }

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

2) Empty String Check with Java 5 and below- Using length()

This is the universal solution and works in all versions of Java, from JDK 1.0 to Java 15. This method highly recommended because of the portability advantage it provides. It is also the fastest way to check if String is empty in Java or not.

/****************************************************************************************
 * Created on 06-2020 Copyright(c) https://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.java.lang;

/**
 * Created by https://kodehelp.com
 * Date: 06/22/2020
 */
public class StringNullEmptyCheck {
    public static void main(String[] args) {
        String string1 = null;
        String string2 = "";

        if(isStringEmpty(string1)){
            System.out.println("string1 is Empty!");
        }

        if(isStringEmpty(string2)){
            System.out.println("string2 is Empty!");
        }
    }

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

3) Blank String check with Java 10 and below

You can use below solution if your program consider a String with only white-space as a blank String.

/****************************************************************************************
 * Created on 06-2020 Copyright(c) https://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.java.lang;

/**
 * Created by https://kodehelp.com
 * Date: 06/22/2020
 */
public class StringNullEmptyCheck {
    public static void main(String[] args) {
        String string1 = null;
        String string2 = "      ";

        if(isStringEmpty(string1)){
            System.out.println("string1 is Empty!");
        }

        if(isStringEmpty(string2)){
            System.out.println("string2 is Empty!");
        }
    }

    static boolean isStringEmpty(String string){
        return string==null || string.trim().length() == 0;
    }
}

4) Blank String check with Java 11 and above

Java 11 introduced isBlank() method to check if the string is empty or contains whitespace characters. This makes more straight forward t o check if the string is Empty or Blank. Below example shows how use the isBlank() method.

/****************************************************************************************
 * Created on 06-2020 Copyright(c) https://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.java.lang;

/**
 * Created by https://kodehelp.com
 * Date: 06/22/2020
 */
public class StringNullEmptyCheck {
    public static void main(String[] args) {
        String string1 = null;
        String string2 = "      ";

        if(isStringEmpty(string1)){
            System.out.println("string1 is Empty!");
        }

        if(isStringEmpty(string2)){
            System.out.println("string2 is Empty!");
        }
    }

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

You can also use Google Guava, Spring, or Apache commons to check for Empty or Blank string. Remember, Joshua Bloch has advised in Effective Java to learn and use library functions, whenever possible.

References:

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *