What does String.isBlank() do (Java 11), and how does it differ from isEmpty()?
answer
- isEmpty = length 0
- isBlank = empty OR all whitespace (Java 11)
- isBlank ≈ strip().isEmpty(), but no allocation
- uses Character.isWhitespace (U+00A0 not blank)
- not null-safe — still need a null check
basics
~20 sisEmpty() is true only when the string has length 0. isBlank() (Java 11) is true when the string is empty OR contains only whitespace characters. So " ".isBlank() is true but " ".isEmpty() is false.
solid answer
~40 sisEmpty(), present since Java 6, returns true only if the string's length is zero. isBlank(), added in Java 11, returns true if the string is empty or contains only whitespace, where whitespace is defined by Character.isWhitespace() (the same Unicode-aware definition strip() uses). It is essentially equivalent to s.strip().isEmpty() but without allocating a new string, so it is the idiomatic, allocation-free way to check for "effectively empty" input. Typical use: validating that a user actually typed something meaningful, since a field of just spaces should usually be treated as empty. Because isBlank() uses Character.isWhitespace, the non-breaking space U+00A0 is NOT considered blank, mirroring strip()'s behavior.
go deeper
Knows isEmpty() checks length 0 and isBlank() also returns true for whitespace-only strings.
Explains that isBlank() uses Character.isWhitespace and is equivalent to strip().isEmpty() without allocation; uses it for input validation.
Notes null-safety (instance method, can NPE), the NBSP quirk, and that isBlank() replaces the old trim().isEmpty()/Commons StringUtils pattern.
Sets validation conventions across a codebase (e.g. a shared notBlank helper), weighing null handling, NBSP normalization, and avoiding third-party deps for trivial checks.
## The two checks When working with text you often want to know "is there anything here?" There are two related but distinct notions: 1. **Empty** — the string contains zero characters, i.e. `length() == 0`. The string literal `""`. 2. **Blank** — the string contains no *visible* content: it is either empty OR made up entirely of whitespace characters (spaces, tabs, newlines, Unicode spaces). ## isEmpty() `String.isEmpty()` (since Java 6) is simply `length() == 0`. `"".isEmpty()` is true; `" ".isEmpty()` is false because that string has one character (a space). ## isBlank() (Java 11) `String.isBlank()` returns true if the string is empty **or** every character in it satisfies `Character.isWhitespace(codePoint)`. So: - `"".isBlank()` -> true - `" ".isBlank()` -> true (only spaces) - `"\t\n".isBlank()` -> true (tab + newline) - `" x ".isBlank()` -> false (contains a non-whitespace 'x') ### Relationship to strip() `s.isBlank()` is logically the same as `s.strip().isEmpty()`, because if stripping all leading/trailing whitespace leaves nothing, the whole string was whitespace. The difference: `isBlank()` does NOT allocate a new String — it scans the existing one — so it is cheaper and clearer. Prefer `isBlank()` over `strip().isEmpty()`. ### The Unicode whitespace definition (and the U+00A0 gotcha) Because `isBlank()` uses `Character.isWhitespace()`, it shares strip()'s quirk: the non-breaking space U+00A0 is NOT whitespace by that definition, so a string of only non-breaking spaces is NOT blank. If you ingest HTML, normalize NBSP first. ## Why it exists / common use The classic use is input validation. A required form field filled with spaces should be rejected as if empty. Before Java 11 people wrote `s == null || s.trim().isEmpty()` (and pulled in Apache Commons `StringUtils.isBlank` for null-safety). Now the JDK has `isBlank()` natively — though note it is an instance method, so you still need a null check separately: `s != null && !s.isBlank()` to require real content. ## Deriving the answer at any level Key facts: isEmpty = length 0; isBlank = empty or all-whitespace (Character.isWhitespace), equivalent to strip().isEmpty() but no allocation; not null-safe; shares the U+00A0 quirk. From those you can answer at any depth.
- How would you check that a required field has real content, null-safely?`s != null && !s.isBlank()`. isBlank() itself is an instance method and will throw NullPointerException on a null reference.
- Why prefer isBlank() over strip().isEmpty()?isBlank() gives the same result but scans the string in place without allocating a new trimmed String, so it is cheaper and more expressive.
saying these in an interview costs you the question
- Saying isBlank() returns true for a null string (it would NPE)
- Claiming isBlank() and isEmpty() are interchangeable
- Thinking " ".isEmpty() is true
- Assuming isBlank() treats the non-breaking space as blank