In Java, isBlank() returns true if the string is empty or every character in it is whitespace, such as spaces, tabs, and line breaks. " ".isBlank() is true, while " x ".isBlank() is false.
Whitespace is decided by Character.isWhitespace, the same rule strip() uses, so s.isBlank() gives the same answer as s.strip().isEmpty() without building a new string. It is the natural check for required form fields.
isBlank() is an instance method of java.lang.String, added in Java 11. Call it on a string, as in text.isBlank(); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed. Requires Java 11 or newer.
Syntax
Call it with no arguments; the result is a boolean.
java
if (comment.isBlank()) {
System.out.println("Comment is required");
}
Call
What it does
"".isBlank()
true.
" \t\n".isBlank()
true: only whitespace.
" a ".isBlank()
false.
Compare with isEmpty(), which is true only for length 0. Every empty string is blank, but not every blank string is empty.
Library declarations
java
public boolean isBlank();
Parameters
Parameter
Description
None
isBlank() takes no parameters.
Return Value
Return Value
Description
boolean
true if the string is empty or contains only whitespace code points.
Java Examples
Example 1 — isBlank() Next to isEmpty()
Four inputs show where the two checks agree and where they differ.
java
public class IsBlankVsIsEmpty {
public static void main(String[] args) {
String[] samples = {"", " ", "\t\n", " x "};
for (String s : samples) {
System.out.println("length " + s.length() + ": isEmpty=" + s.isEmpty() + ", isBlank=" + s.isBlank());
}
}
}