Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

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
false

Checking for null and emptiness together:

void main() {
    String name = null;
    if (name == null || name.isEmpty()) {
        IO.println("No name");
    }
}

Output:

No name

Notes

A space is a character, so " ".isEmpty() is false. If spaces should count as empty, use isBlank().

See also

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.

Thanks for reading!

Donate to the site — we work hard for you.