Java.lang Package Classes

Java String - isEmpty() Method



The java.lang.String.isEmpty() method is used to check whether the specified string is empty or not. It returns true when the string is empty, else returns false.

Syntax

public boolean isEmpty()

Parameters

No parameter is required.

Return Value

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

Exception

NA.

Example:

In the example below, isEmpty() method is used to check whether the string called MyString is empty or not.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    String MyString = "Hello World!.";
    System.out.println(MyString.isEmpty()); 

    MyString = "   ";
    System.out.println(MyString.isEmpty()); 

    MyString = "";
    System.out.println(MyString.isEmpty());  
  }
}

The output of the above code will be:

false
false
true

❮ Java.lang - String