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());true
false
tusciasstores"", so its length is0.tuscias.isEmpty()returnstrue.tekstascontains characters, soisEmpty()returnsfalse.
Method syntax
public boolean isEmpty()
| Part | Meaning |
|---|---|
| Calling object | A specific String, for example, tekstas.isEmpty(). |
| Parameters | No parameters. |
| Return type | boolean. |
Returns true | When length() is 0. |
Returns false | When 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);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());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());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());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);
}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());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());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 ofisBlank()when whitespace-only input must also be rejected.- Forgetting that
isEmpty()performs the same logical check aslength() == 0. isEmpty()is called through anullvalue.- 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().