In this short article, you will get to know about the difference between the isBlank() and isEmpty() methods of the String class in Java. In Java , there are two methods to check whether a string is valid or not? Those methods are isBlank() and isEmpty().

Let's see the comparison between the isBlank() and isEmpty() method of the Java String class.


Internal implementation of isBlank()

public boolean isBlank() {
    return indexOfNonWhitespace() == length();
}

Internal implementation of isEmpty()

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

Differences between isBlank() vs isEmpty()

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

isBlank()isEmpty()
Added in the JDK 11 release.Added in the JDK 1.6 release.
Returns true if a string is empty or only contains white space otherwise returns false.Returns true if the length of the string is 0, otherwise returns false.
It uses Character.isWhitespace(int) 
method to determine a white space character.
It uses the length() method to determine the emptiness of a string.
When isBlank() is true then isEmpty() may or may not be true, but when isBlank() is false then isEmpty() will always be false. When isEmpty() is true then isBlank() will always be true, but if isEmpty() is false then isBlank() may or may not be false.

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 String doesn’t have character, and it returns false if at least 1 character available in this string object.


Return values of isBlank() method,

It uses Character.isWhitespace(char) method to determine a white space character.

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 String doesn’t have characters or it has only spaces/tabs. Otherwise, it returns false


Program examples to demonstrate isBlank() vs isEmpty()

Example1:- 

String name = "";
System.out.println(name.isBlank()); // return --> true
System.out.println(name.isEmpty()); // return --> true

Example2:- 

String name = " ";
System.out.println(name.isBlank()); // return --> true
System.out.println(name.isEmpty()); // return --> 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.

Example3:- 

String name = "\u2005";
System.out.println(name.isBlank()); // return --> true
System.out.println(name.isEmpty()); // return --> false

Example4:- 

String name = "  ";
System.out.println(name.isBlank()); // return --> true
System.out.println(name.trim().isEmpty()); // return --> true

Example5:- 

String name = "\u2005";
System.out.println(name.isBlank()); // return --> true
System.out.println(name.trim().isEmpty()); // return --> false

Note: trim() is not aware of Unicode whitespace characters and hence does not consider ‘\u2005′ a whitespace character.