Java String isEmpty() method checks whether current String is empty or not.
Syntax:
1 2 3 |
public boolean isEmpty() |
Returns
It returns true, if length of String is 0 otherwise false.
Java String isEmpty() method example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class JavaStringIsEmptyEx1 { public static void main(String args[]) { String s1="TutorialAndExample"; String s2=""; String s3=s2.concat(""); System.out.println(s1.isEmpty()); System.out.println(s2.isEmpty()); System.out.println(s3.isEmpty()); } } |
Output:
1 2 3 4 5 |
false true true |
Java String isEmpty() method example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class JavaStringIsEmptyEx2 { public static void main(String[] args) { String s1="TutorialAndExample"; String s2=""; // Either length is zero or isEmpty is true if(s1.length()==0 || s1.isEmpty()) System.out.println("String s1 is empty"); else System.out.println("s1"); if(s2.length()==0 || s2.isEmpty()) System.out.println("String s2 is empty"); else System.out.println(s2); } } |
Output:
1 2 3 4 |
s1 String s2 is empty |
Java String isEmpty() method example 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class JavaStringIsEmptyEx3 { public static void main(String[] args) { String first_Name="Nitish"; String last_Name=""; if(first_Name.length()==0 || first_Name.isEmpty()) System.out.println("Please provide First Name "); else System.out.println("First Name : "+first_Name); if(last_Name.length()==0 || last_Name.isEmpty()) System.out.println("Please provide last Name"); else System.out.println("Last Name : "+last_Name); } } |
Output:
1 2 3 4 |
First Name : Nitish Please provide last Name |