Java String isEmpty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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");
}
CallWhat 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

ParameterDescription
NoneisEmpty() takes no parameters.

Return Value

Return ValueDescription
booleantrue 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());
        }
    }
}

How It Works

  1. Brackets show the content. Wrapping each value in [ ] makes the empty string and the lone space visible in the output.
  2. Only length 0 is empty. "" answers true; the single space has length 1, so it answers false.
  3. Matching numbers. The printed lengths confirm the rule: isEmpty() is true exactly when the length is 0.

Example 2 — Write a Null-Safe Empty Check

Handle missing values before calling isEmpty().

java
public class IsEmptyNullSafe {
    static boolean isNullOrEmpty(String s) {
        return s == null || s.isEmpty();
    }

    public static void main(String[] args) {
        String[] inputs = {null, "", "  ", "ok"};

        for (String in : inputs) {
            System.out.println("[" + in + "] -> " + isNullOrEmpty(in));
        }
    }
}

How It Works

  1. Short-circuit protection. For null, s == null is already true, so || never calls isEmpty() and no exception occurs.
  2. Concatenation prints null. Joining null into a string writes the word null, which is why the first line shows [null].
  3. 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);
    }
}

How It Works

  1. What split() keeps. "a,,b,".split(",") gives a, an empty string from ",,", and b; the trailing empty piece is dropped, so there are 3.
  2. Test each piece. p.isEmpty() is true only for the piece between the two commas.
  3. 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.

Next: join()

Join several strings with a delimiter between them (static).

join() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful