Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Program to check if the String is Empty in Java

  • Last Updated : 22 Jan, 2020

Given a string str, the task is to check if this string is empty or not, in Java.

Examples:

Input: str = "" 
Output: True

Input: str = "GFG"
Output: False

Approach:

  • Get the String to be checked in str
  • We can simply check if the string is empty or not using the isEmpty() method of String class
    Syntax:
    if (str.isEmpty())
    
  • Print true if the above condition is true. Else print false.

Below is the implementation of the above approach:




// Java Program to check if
// the String is empty in Java
  
class GFG {
  
    // Function to check if the String is empty
    public static boolean isStringEmpty(String str)
    {
  
        // check if the string is empty or not
        // using the isEmpty() method
        // and return the result
        if (str.isEmpty())
            return true;
        else
            return false;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String str1 = "GeeksforGeeks";
        String str2 = "";
  
        System.out.println("Is string \"" + str1
                           + "\" empty? "
                           + isStringEmpty(str1));
        System.out.println("Is string \"" + str2
                           + "\" empty? "
                           + isStringEmpty(str2));
    }
}
Output:
Is string "GeeksforGeeks" empty? false
Is string "" empty? true
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!