java.lang.String isEmpty()

Description :

This java tutorial shows how to use the isEmpty() method of java.lang.String class. This method returns boolean data type which signifies if the String length is 0 or not. This method generally checks if the string is empty (string length is 0).

Method Syntax :

public boolean isEmpty()

Method Returns :

This method returns boolean datatype depending if the String length is zero or not. This method returns true if and only if the length of the String is zero, otherwise false.

Compatibility Version:

Requires Java 1.6 and up

Exception :

No Exception is expected as specified on java api.

Java Code Example :

This example source code demonstrates the use of isEmpty() method of String class. We have also added on this java code that checks the length of the String.

package com.javatutorialhq.tutorial;

/*
 * This java example source code that checks if the String length is 0 or not (using isEmpty method)
 */

public class StringEmptyDemo {

    public static void main(String[] args) {
        String input = "This is example demo of isEmpty method";
        if(input.isEmpty()){
            System.out.println("String input is empty with length "+input.length());
        }
        else{
            System.out.println("String input is not empty with length "+input.length());
        }
        System.out.println("".isEmpty());
    }

}

Sample Output :

Running the isEmpty() example source code will give you the following output

java string isempty method example

java string isempty method example

Exception Scenario :

No Exception specified on Java API

Suggested Reading List :