Easy Tutorial
❮ Java Enum File Date Modify ❯

Java String isEmpty() Method

Java String Class


The isEmpty() method is used to check if a string is empty.

Syntax

public boolean isEmpty()

Parameters

Return Value

Returns true if the string is empty, otherwise returns false.

A string is considered empty if its length, calculated by the length() method, is 0.

Example

The following example demonstrates the use of the isEmpty() method:

Example

public class Main {
    public static void main(String[] args) {
        String myStr1 = "tutorialpro";  
        String myStr2 = "";        // Empty string
        String myStr3 = "    ";    // Multiple spaces, length() is not 0 
        System.out.println("Is myStr1 empty: " + myStr1.isEmpty());
        System.out.println("Is myStr2 empty: " + myStr2.isEmpty());
        System.out.println("Length of myStr3: " + myStr3.length());
        System.out.println("Is myStr3 empty: " + myStr3.isEmpty());
    }
}

The output of the above program is:

Is myStr1 empty: false
Is myStr2 empty: true
Length of myStr3: 4
Is myStr3 empty: false

Java String Class

❮ Java Enum File Date Modify ❯