isBlank() vs isEmpty() in Java String

isBlank() vs isEmpty() in Java String | In the String class of Java, there are two methods to check if a string is valid or not. Those methods are isBlank() and isEmpty(). In their respective topics, we have already seen them in detail. Now, let us see the comparison between the isBlank() and isEmpty() methods of the Java String class.

Internal implementation of isBlank() vs isEmpty()

public boolean isBlank() {
    return indexOfNonWhitespace() == length();
}
public boolean isEmpty() {
   return value.length == 0;
}

Return value of String isEmpty() method,

true:- If the length of the string is 0.
false:- If the length of the string is greater than 0.

This method was given in Java 1.6 version. It returns true if the String doesn’t have a character, and it returns false if at least 1 character is available in this string object.

Return values of isBlank() method,

true:- If the string is empty or contains only whitespace (space, tab).
false:- If the string is not empty and contains valid characters except white space.

This method was given in the Java 11 version. This method returns true if the String doesn’t have characters or it has only spaces/tabs. Otherwise, it returns false

Differences between isBlank() vs isEmpty()

The below table shows the differences between the isBlank() and isEmpty() methods given in the String class,

isEmpty()isBlank()
It was introduced in the Java 1.6 version.It was introduced in the Java 11 version.
It returns true if the length of the string is zero (0), else it returns false.It returns true if the length of the string is zero (0) or it contains only whitespaces, else it returns false.
It only checks the length of the string.It checks the length and characters of the string.
When isEmpty() is true then isBlank() will always be true, but if isEmpty() is false then isBlank() may or may not be false.When isBlank() is true then isEmpty() may or may not be true, but when isBlank() is false then isEmpty() will always be false.

Note:- An empty string must be blank but a blank string may or may not be empty. It means when isEmpty() is true then the isBlank() method will always give true, but the reverse isn’t true.

Program to demonstrate isBlank() vs isEmpty()

public class Test {
   public static void main(String[] args) {

      String s1 = "";
      System.out.println(s1.isBlank() + " " + s1.isEmpty());

      String s2 = " ";
      System.out.println(s2.isBlank() + " " + s2.isEmpty());

      String s3 = "Program";
      System.out.println(s3.isBlank() + " " + s3.isEmpty());

      String s4 = new String();
      System.out.println(s4.isBlank() + " " + s4.isEmpty());

      String s5 = new String("");
      System.out.println(s5.isBlank() + " " + s5.isEmpty());

      String s6 = new String(" ");
      System.out.println(s6.isBlank() + " " + s6.isEmpty());

      String s7 = new String("Program");
      System.out.println(s7.isBlank() + " " + s7.isEmpty());
   }
}

The output of the above program:-

true true
true false
false false
true true
true true
true false
false false

The string literal ” ” is a valid string of length 1 therefore s2.isEmpty() method returns false, but it contains only white spaces therefore s2.isBlank() method returns true.

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 *