STRING INSTANCE METHODS

Java String isBlank() method

isBlank() checks whether a string is empty or contains only whitespace, such as spaces, tabs or newlines. It returns truewhen the text has no visible content.

Your first isBlank() call

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

System.out.println(tuscias.isBlank());
System.out.println(tarpai.isBlank());
System.out.println(tekstas.isBlank());
Console output

true
true
false

  1. tuscias stores an empty string, so isBlank() returns true.
  2. tarpai contains only spaces and is therefore also considered blank.
  3. tekstas contains visible letters, so the result is false.

Method syntax

public boolean isBlank()

Parts of the isBlank() method
PartMeaning
Calling objectA specific String, for example, tekstas.isBlank().
ParametersNo parameters.
Return typeboolean.
Returns trueWhen the string is empty or contains only whitespace.
Returns falseWhen it contains at least one non-whitespace character.
Available since which Java version?isBlank() was introduced in Java 11.

Java uses the Character.isWhitespace(int) rule, considering other whitespace characters as well as ordinary spaces.

Why is isBlank() an instance method?

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

boolean tuscias = tekstas.isBlank();

String ivestis = "   ";

boolean tusciaArbaTikTarpai = ivestis.isBlank();

System.out.println(tusciaArbaTikTarpai);
Console output

true

We therefore do not write String.isBlank(...). The method needs a specific String value to check.

Spaces, tabs and newlines

String ivestis = " \t\n ";

System.out.println(ivestis.isBlank());
Console output

true

A string can contain characters yet still be blank if they are all whitespace: a space, \t tab or \n newline. isBlank() treats such text as blank.

The difference from isEmpty()

String tarpai = "   ";

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

false
true

isEmpty() checks only length: if length() is 0, the result is true. The text " " has three characters, so isEmpty() returns false. isBlank() examines the contents and recognizes only spaces.

Spaces around text do not make it blank

String pirmas = " Java ";
String antras = "Mokausi Java";

System.out.println(pirmas.isBlank());
System.out.println(antras.isBlank());
Console output

false
false

If a string contains at least one letter, digit or other non-whitespace character, isBlank() returns false. Leading and trailing spaces do not make it blank when it contains actual content.

Validating user input

String vardas = "   ";

if (vardas.isBlank()) {
  System.out.println("Vardas privalomas");
} else {
  System.out.println("Sveiki, " + vardas);
}
Console output

Vardas privalomas

isBlank() is well suited to forms and command input. A user may enter nothing or only spaces. Programs usually need to treat both as missing input.

How was this done before Java 11?

String ivestis = "   ";

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

true
true

Before Java 11, code often used trim().isEmpty(). This removes leading and trailing spaces, then checks whether anything remains. Java 11 introduced the clearer method isBlank(), often a better choice in modern code.

The method cannot be called on null

String tekstas = null;

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

NullPointerException

isBlank() 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

  • isEmpty() is used where whitespace-only values must also be rejected.
  • Assuming that " " is empty according to isEmpty()although its length is not 0.
  • Forgetting that isBlank() was introduced only in Java 11.
  • isBlank() is called through a null value.
  • Confusing blank text with text that has spaces around real content.

When should you use isBlank()?

Use isBlank()when checking whether text is effectively unfilled: empty or containing only spaces, tabs or newlines. This is useful for user input, required fields, configuration values and commands. If you only care whether String has length 0, use isEmpty().