March 2, 2022

Check if a String is Null or Empty in Java

To check if a String is null or empty in Java you can use one of the following options.

  1. Use isEmpty() method available Java 6 onward to check if the String is empty.
  2. Use StringUtils.isEmpty() method of the Apache Commons Lang.
  3. From Java 11 onward there is also isBlank() method to check if the String is empty or contains only white spaces.

Check String empty or null using isEmpty()

In order to check if String empty or null in Java you should first check whether the String is null or not then check if it is empty or not. If you use or condition (||) then second part is not evaluated if the first part itself is true, so you won’t be calling isEmpty() method on a null String if String itself is null. For example-

if(str == null || str.isEmpty())

If String is null then first part of the condition itself evaluates to true and second part is not checked.

public class StringEmpty {
  public static void main(String[] args) {
    String str1 = "A String";
    String str2 = "";
    // evaluates to false
    if(isNullOrEmpty(str1)) {
      System.out.println("String str1 is empty");
    }
    // evaluates to true
    if(isNullOrEmpty(str2)) {
      System.out.println("String str2 is empty");
    }	
		
    // evaluates to true
    if(isNotNullOrEmpty(str1)) {
      System.out.println("String str1 is not empty");
    }
    // evaluates to false
    if(isNotNullOrEmpty(str2)) {
      System.out.println("String str2 is not empty");
    }	
  }
	
  // Method returns true if String is null or empty
  private static boolean isNullOrEmpty(String str){
    if(str == null || str.isEmpty())
      return true;
    return false;
  }
	
  // Method returns true if String is not null or empty
  private static boolean isNotNullOrEmpty(String str){
    if(str != null && !str.isEmpty())
      return true;
    return false;
  }
}
Output
String str2 is empty
String str1 is not empty

In the example two methods are given, first returns true if the String is null or empty where as second method returns true if the string is not null and not empty.

Apache Commons Lang StringUtils.isEmpty() method

In StringUtils utility class of the Apache Commons Lang there is a method isEmpty() to check if the String is empty. This method checks for null String too so you don’t need to do that null check.

public class StringEmpty {
  public static void main(String[] args) {
    String str1 = "A String";
    String str2 = null;
    if(StringUtils.isEmpty(str1)) {
      System.out.println("String str1 is empty");
    }		
    if(StringUtils.isEmpty(str2)) {
      System.out.println("String str2 is empty");
    }
  }
}
Output
String str2 is empty

Java String isBlank() method

Java 11 onward String class in Java has a isBlank() method that returns true if the string is empty or contains only white spaces, false otherwise.

public class StringEmpty {
  public static void main(String[] args) {
    String str1 = "A String";
    String str2 = "   ";
    // evaluates to false
    if(isNullOrSpaces(str1)) {
      System.out.println("String str1 is empty or has spaces");
    }
    // evaluates to true
    if(isNullOrSpaces(str2)) {
      System.out.println("String str2 is empty or has spaces");
    }
  }
	
  // Method returns true if String is null or empty
  private static boolean isNullOrSpaces(String str){
    if(str == null || str.isBlank())
      return true;
    return false;
  }
}
Output
String str2 is empty or has spaces

As you can see with isBlank() it also checks if String has only spaces.

That's all for the topic Check if a String is Null or Empty in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment