isEmpty() returns true only when the length is 0. isBlank() (Java 11) returns true when the String is empty or contains only whitespace. Neither handles null, because they are instance methods, so you need a null check first or a helper method.
Key Points: • "".isEmpty() and "".isBlank() are both true. • " ".isEmpty() is false, but " ".isBlank() is true. • isBlank() uses Character.isWhitespace(), so it also treats tabs and newlines as blank. • Null-safe check: s == null || s.isBlank(). Put the null check first so it short-circuits. • Libraries offer the same: StringUtils.isBlank() in Apache Commons Lang and StringUtils.hasText() in Spring (the opposite meaning). • For input validation in Spring Boot, @NotBlank on the DTO field does this check for you.
Code Example:
public class BlankChecks {
static boolean isNullOrBlank(String s) {
return s == null || s.isBlank();
}
public static void main(String[] args) {
String[] values = {"", " ", "\t\n", "java", null};
for (String v : values) {
String label = v == null ? "null" : "\"" + v.replace("\t", "\\t").replace("\n", "\\n") + "\"";
String empty = v == null ? "NPE" : String.valueOf(v.isEmpty());
String blank = v == null ? "NPE" : String.valueOf(v.isBlank());
System.out.println(label + " isEmpty=" + empty + " isBlank=" + blank
+ " isNullOrBlank=" + isNullOrBlank(v));
}
}
}Output:
"" isEmpty=true isBlank=true isNullOrBlank=true " " isEmpty=false isBlank=true isNullOrBlank=true "\t\n" isEmpty=false isBlank=true isNullOrBlank=true "java" isEmpty=false isBlank=false isNullOrBlank=false null isEmpty=NPE isBlank=NPE isNullOrBlank=true
Good to Know: Before Java 11, developers wrote s.trim().isEmpty(). trim() removes only characters up to the space character (U+0020), while isBlank() and strip() are Unicode-aware, so they also handle spaces such as the non-breaking space found in copied text.
Interview Tip: A concise interview answer is:
"isEmpty returns true only for a zero-length string, while isBlank, from Java 11, is also true for strings that contain only whitespace. Both are instance methods, so they throw on null; I check s == null || s.isBlank(), or use @NotBlank for request validation in Spring Boot."
Previous question: What is the difference between String.valueOf(), toString() and concatenating with "" when converting to a String?
Next question: What are the common pitfalls of String.split() in Java?