StringUtils isEmpty() Method

StringUtils isEmpty() Method | In this blog, we will discuss the isEmpty() method, which is the static method present in the StringUtils class. We can use this method to check whether the string is empty or not. We consider the string is empty only when it satisfies the below criteria:- 

  1. If the size or length of the string is zero.
  2. If the string points to a null reference.

Java StringUtils isEmpty() Method

The syntax of isEmpty() is as follows:-
public static boolean isEmpty(final CharSequence cs)
It checks if the CharSequence is null or empty.

Parameters: CharSequence cs, the string needed to be checked.
Return type: boolean.
Returns: true or false.

StringUtils isEmpty() Example,

Method CallResult
StringUtils.isEmpty(“Hello”)false
StringUtils.isEmpty(null)true
StringUtils.isEmpty("")true
StringUtils.isEmpty("  ")false

StringUtils.isEmpty() Method

To use any methods of StringUtils we need to import the StringUtils from apache as import org.apache.commons.lang3.StringUtils. In the below program, we have used the isEmpty() method for the five different strings so that we can understand the isEmpty() method more clearly.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "Good morning";
        System.out.println("StringUtils.isEmpty() = " + StringUtils.isEmpty(string));

        string = null;
        System.out.println("StringUtils.isEmpty() = " + StringUtils.isEmpty(string));

        string = "null";
        System.out.println("StringUtils.isEmpty() = " + StringUtils.isEmpty(string));

        string = "    \n\t";
        System.out.println("StringUtils.isEmpty() = " + StringUtils.isEmpty(string));

        string = "";
        System.out.println("StringUtils.isEmpty() = " + StringUtils.isEmpty(string));
    }
}

Output:

StringUtils.isEmpty() = false
StringUtils.isEmpty() = true
StringUtils.isEmpty() = false
StringUtils.isEmpty() = false
StringUtils.isEmpty() = true

The string “Good morning” has a length greater than 0 including the whitespaces therefore StringUtils.isEmpty() method returns false. The StringUtils.isEmpty() doesn’t trim the given string, and therefore if the string contains only whitespaces then also it will return false. Also see:- StringUtils isBlank() Method

String class isEmpty() vs StringUtils.isEmpty() Method

Java language, String class also contains isEmpty() method to check whether the given string is empty or not, but it has the following main difference from StringUtils.isEmpty() method:-

Since String class isEmpty() method is an instance or non-static method therefore to call the isEmpty() method string object must be there. If the string is referring to null, in that case, we will get NullPointerException.

Whereas StringUtils.isEmpty() method is a static method and therefore if we pass a string referring to null then it will return true instead of NullPointerException. The below example demonstrates it:-

import org.apache.commons.lang3.StringUtils;

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

Output:-

StringUtils.isEmpty() = true
String isEmpty() =
Exception in thread “main” java.lang.NullPointerException
at Main.main(Main.java:8)

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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