Home >>Java String Methods >Java String isEmpty() Method

Java String isEmpty() Method

Java String isEmpty() Method

The isEmpty() method in Java String is used to checks whether a string is empty or not.

Syntax

public boolean isEmpty()

Parameters

None

Technical Details

Returns- It returns a boolean value:

  • true - It returns true, if the string is empty (length() is 0)
  • false - It returns false, if the string is not empty

Java String isEmpty() Method Example 1


public class Isemptyexample1 
{
  public static void main(String[] args) 
  {
    String Name1 = "Hey";
    String Name2 = "";
    System.out.println(Name1.isEmpty());
    System.out.println(Name2.isEmpty());
  }
}

Output:
false
true

Java String isEmpty() Method Example 2


public class IsEmptyExample2 
{  
    public static void main(String[] args) 
	{  
        String m1="";    
        String m2="Phptpoint";             
        if(m1.length()==0 || m1.isEmpty())  
            System.out.println("m1 String is empty");  
        else System.out.println("m1");        
        if(m2.length()==0 || m2.isEmpty())  
            System.out.println("m2 String is empty");  
        else System.out.println(m2);  
    }  
}  

Output:
m1 String is empty
phptpoint