Java 25 (LTS) reference · String Methods

Java String isBlank() Method

Return true if the string is empty or contains only whitespace. isEmpty() is true only for "".

Java 25 (LTS) Since Java 11

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.isBlank()

Return value

true or false.

Examples

isBlank() vs isEmpty()
public class Main {
    public static void main(String[] args) {
        System.out.println("".isBlank());
        System.out.println("   ".isBlank());
        System.out.println("   ".isEmpty());
        System.out.println(" a ".isBlank());
    }
}

Output

true
true
false
false

Run this on your computer — see “Get Started” for setup.

A default for blank input
public class Main {
    public static void main(String[] args) {
        String name = "  ";
        String display = name.isBlank() ? "Anonymous" : name.strip();
        System.out.println(display);
    }
}

Output

Anonymous

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
Java Java 11Added

Was this page helpful?