Java - String isEmpty() Method



The Java String isEmpty() method is used to check whether the current string is empty or not. The method returns a boolean value which is true if and only if the string is empty; false otherwise. The isEmpty() method does not accept any parameter.

Note: If the string is, initialized as null, the isEmpty() method throws a NullPointerException. Because Null is a placeholder that generally means, "no data is available about this”, and it is not the same as an empty string, the compiler cannot handle it and throws a NullPointerException.

Syntax

Following is the syntax of the Java String isEmpty() method −

public boolean isEmpty()

Parameters

  • It does not accept any parameter.

Return Value

This method returns true if length() is 0, else false.

Example

If the given string is empty, the isEmpty() method returns true.

In the following program, we are creating a string literal with an empty value. Then, using the isEmpty() method, we are, trying to check whether the current string is an empty string or not.

import java.lang.*;
public class CheckEmpty {
   public static void main(String[] args) {
      
      //create a string literal
      String str = "";
      System.out.println("The given string is an empty." + str);
      System.out.println("Length of the string is: " + str.length());
      
      //using the isEmpty() method
      System.out.println("The current string is an empty string or not? " + str.isEmpty());
   }
}

Output

On executing the above program, it will produce the following result −

The given string is an empty.
Length of the string is: 0
The current string is an empty string or not? true

Example

If the given string is non-empty, this method returns false.

In the following example, we are instantiating the string class with the value “TutorialsPoint”. Using the isEmpty() method, we are trying to check whether this string is empty or not.

import java.lang.*;
public class CheckEmpty {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("TutorialsPoint");
      System.out.println("The given string is: " + str);
      System.out.println("Length of the string is: " + str.length());
      
      //using the isEmpty() method
      System.out.println("The current string is an empty string or not? " + str.isEmpty());
   }
}

Output

Following is the output of the above program −

The given string is: TutorialsPoint
Length of the string is: 14
The current string is an empty string or not? false

Example

Using the conditional statement to check whether the current string is empty or not.

In this program, we create an object of the string class with the value “Hello”. Then, using the isEmpty() method and conditional statement, we are trying to check whether the current string is empty or not.

import java.lang.*;
public class CheckEmpty {
   public static void main(String[] args) {
      
      //create an object of the string class
      String str = new String("Hello");
      System.out.println("The given string is: " + str);
      System.out.println("Length of the string is: " + str.length());
      
      //using the isEmpty() method
      boolean bool = str.isEmpty();
      if(bool) {
         System.out.println("The string is an empty string.");
      } else {
         System.out.println("The string is not an empty string");
      }
   }
}

Output

The above program, produces the following output −

The given string is: Hello
Length of the string is: 5
The string is not an empty string

Example

If the given string is null, this method throws the NullPointerException.

In the following program, we are creating a string literal with the null value. Using the isEmpty() method, we are trying to check whether the given string is empty or not. Since the given string is null, this method throws an exception.

import java.lang.*;
public class CheckEmpty {
   public static void main(String[] args) {
      try {
         
         //create the string literal
         String str = null;
         System.out.println("The given string is: " + str);
         System.out.println("Length of the string is: " + str.length());
         
         //using the isEmpty() method
         boolean bool = str.isEmpty();
         if(bool) {
            System.out.println("The string is an empty string.");
         } else {
            System.out.println("The string is not an empty string");
         }
      } catch(NullPointerException  e) {
         System.out.println("Exception: " + e);
      }
   }
}

Output

After executing the above program, it will produce the following output −

The given string is: null
Exception: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
java_lang_string.htm
Advertisements