String.isEmpty()
Checks whether the string length is zero.
Available since: Java 6
Syntax
public boolean isEmpty()Return value
booleantrue if length() is 0.
Examples
An empty string and a string with a space:
void main() {
IO.println("".isEmpty());
IO.println(" ".isEmpty());
}Output:
true
falseChecking for null and emptiness together:
void main() {
String name = null;
if (name == null || name.isEmpty()) {
IO.println("No name");
}
}Output:
No nameNotes
A space is a character, so " ".isEmpty() is false. If spaces should count as empty, use isBlank().
See also
- String.isBlank() — Checks whether a string is empty or contains only whitespace.
- String.length() — Returns the number of characters in a string.
Related recipes
In the tutorial
- String methods — The main String methods in Java: searching, substring, changing case, strip, replace, split, join, repeat and string checks.