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());true
true
false
tusciasstores an empty string, soisBlank()returnstrue.tarpaicontains only spaces and is therefore also considered blank.tekstascontains visible letters, so the result isfalse.
Method syntax
public boolean isBlank()
| Part | Meaning |
|---|---|
| Calling object | A specific String, for example, tekstas.isBlank(). |
| Parameters | No parameters. |
| Return type | boolean. |
Returns true | When the string is empty or contains only whitespace. |
Returns false | When 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);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());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());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());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);
}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());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());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 toisEmpty()although its length is not0. - Forgetting that
isBlank()was introduced only in Java 11. isBlank()is called through anullvalue.- 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().