STRING INSTANCE METHODS

Java String isEmpty() method

isEmpty() checks whether the String string is completely empty. It returns true only when the string's length is 0.

Your first isEmpty() call

String tuscias = "";
String tekstas = "JavaQuest";

System.out.println(tuscias.isEmpty());
System.out.println(tekstas.isEmpty());
Console output

true
false

  1. tuscias stores "", so its length is 0.
  2. tuscias.isEmpty() returns true.
  3. tekstas contains characters, so isEmpty() returns false.

Method syntax

public boolean isEmpty()

Parts of the isEmpty() method
PartMeaning
Calling objectA specific String, for example, tekstas.isEmpty().
ParametersNo parameters.
Return typeboolean.
Returns trueWhen length() is 0.
Returns falseWhen the text contains at least one character.
Available since which Java version?isEmpty() was introduced in Java 6.

The key rule: isEmpty() does not check whether text looks empty to a person. It only checks whether its length equals 0.

Why is isEmpty() an instance method?

isEmpty() is called on a specific string because Java must check that String object's length:

boolean arTuscias = tekstas.isEmpty();

String tekstas = "";

boolean arTuscias = tekstas.isEmpty();

System.out.println(arTuscias);
Console output

true

We therefore do not write String.isEmpty(...). The method needs a specific String value whose length can be checked.

isEmpty() and length()

String tuscias = "";
String vardas = "Ada";

System.out.println(tuscias.length());
System.out.println(tuscias.isEmpty());

System.out.println(vardas.length());
System.out.println(vardas.isEmpty());
Console output

0
true
3
false

isEmpty() is a clearer way to write the check tekstas.length() == 0. If the length is 0, the string is empty. If it exceeds 0, the string contains at least one character.

Spaces are not an empty string

String tarpai = "   ";

System.out.println(tarpai.length());
System.out.println(tarpai.isEmpty());
Console output

3
false

The text " " contains three space characters. It may look empty to a person, but Java sees three characters, so isEmpty() returns false.

The difference from isBlank()

String tarpai = "   ";

System.out.println(tarpai.isEmpty());
System.out.println(tarpai.isBlank());
Console output

false
true

isBlank() returns truewhen the string is empty or contains only whitespace. isEmpty() is stricter: it returns true only when there are no characters at all.

Validating user input

String slapyvardis = "";

if (slapyvardis.isEmpty()) {
  System.out.println("Slapyvardis neivestas");
} else {
  System.out.println("Slapyvardis: " + slapyvardis);
}
Console output

Slapyvardis neivestas

isEmpty() suits checking whether a value is exactly "". If users may enter only spaces, it is usually better to use isBlank()because it also rejects values such as " ".

Checking after trim()

String ivestis = "   ";

System.out.println(ivestis.isEmpty());
System.out.println(ivestis.trim().isEmpty());
Console output

false
true

trim() removes leading and trailing spaces. Therefore, " ".trim() becomes "", then isEmpty() returns true. In modern Java, this situation is often simpler with isBlank().

The method cannot be called on null

String tekstas = null;

System.out.println(tekstas.isEmpty());
Result

NullPointerException

isEmpty() is an instance method. If a variable holds null, it refers to no String object, so Java cannot call the method and throws NullPointerException.

Common mistakes

  • Assuming that text containing spaces is empty according to isEmpty().
  • isEmpty() is used instead of isBlank()when whitespace-only input must also be rejected.
  • Forgetting that isEmpty() performs the same logical check as length() == 0.
  • isEmpty() is called through a null value.
  • Automatically using trim()before checking, although that may change the meaning of the original input.

When should you use isEmpty()?

Use isEmpty()when you need to check exactly whether a String has no characters. This suits values where spaces are meaningful, or an explicit check of length() == 0. For user input that must reject whitespace-only values, usually choose isBlank().