In Java, isEmpty() returns true when the string contains no characters at all, which is exactly length() == 0. A string of spaces is not empty.
It reads better than comparing the length yourself or writing "".equals(text). To treat whitespace-only text as empty too, use isBlank() (Java 11).
isEmpty() is an instance method of java.lang.String, added in Java 6 (JDK 1.6). Call it on a string, as in text.isEmpty(); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed.
Syntax
Call it with no arguments; it answers with a boolean.
java
if (name.isEmpty()) {
System.out.println("Please enter a name");
}
Call
What it does
"".isEmpty()
true.
" ".isEmpty()
false: one space is one char.
"abc".isEmpty()
false.
isEmpty() is an instance method, so the string must exist. The common pattern for possibly missing input is s == null || s.isEmpty(); the || stops before isEmpty() runs on null.
Library declarations
java
public boolean isEmpty();
Parameters
Parameter
Description
None
isEmpty() takes no parameters.
Return Value
Return Value
Description
boolean
true if length() is 0, otherwise false.
Java Examples
Example 1 — Empty Versus a Single Space
Print the answer and the length for three typical inputs.
java
public class IsEmptyBasics {
public static void main(String[] args) {
String[] samples = {"", " ", "Java"};
for (String s : samples) {
System.out.println("[" + s + "] empty? " + s.isEmpty() + ", length " + s.length());
}
}
}
Short-circuit protection. For null, s == null is already true, so || never calls isEmpty() and no exception occurs.
Concatenation prints null. Joining null into a string writes the word null, which is why the first line shows [null].
Spaces still count." " has two chars, so the helper reports false; use isBlank() if spaces should count as empty.
Example 3 — Skip the Empty Pieces Left by split()
Splitting text with doubled separators leaves empty strings that you usually want to ignore.
java
public class IsEmptySplit {
public static void main(String[] args) {
String[] parts = "a,,b,".split(",");
System.out.println("pieces: " + parts.length);
int empty = 0;
for (String p : parts) {
if (p.isEmpty()) {
empty++;
} else {
System.out.println("keep " + p);
}
}
System.out.println("skipped empty: " + empty);
}
}
Output
pieces: 3
keep a
keep b
skipped empty: 1
How It Works
What split() keeps."a,,b,".split(",") gives a, an empty string from ",,", and b; the trailing empty piece is dropped, so there are 3.
Test each piece.p.isEmpty() is true only for the piece between the two commas.
Keep the rest. The loop prints a and b and counts one skipped empty string.
Frequently Asked Questions
true when the string has length 0, false otherwise. It is the same as length() == 0.
No. " ".isEmpty() is false because the spaces are characters. Use isBlank() (Java 11) to treat whitespace-only strings as blank.
A NullPointerException. Check with s == null || s.isEmpty() when the value may be missing.
Since Java 6 (JDK 1.6). Before that, code used length() == 0.
Both work for non-null strings. isEmpty() states the intent more clearly; "".equals(s) has the advantage of returning false for null instead of throwing.