The Java String.isEmpty method is one of the Java String Methods, which is to check whether the user-specified string is Empty or Not. If the length of the user-specified string is zero, then the String.isEmpty will return TRUE.
In this article, we will show how to find the Empty strings using a Java string isEmpty method with an example. The basic syntax of the String isEmpty in Java Programming language is as shown below.
public Boolean isEmpty() // It will return Boolean True or False as Output //In order to use in program String_Object.isEmpty()
- String_Object: Please specify the valid String Object.
Java String isEmpty Method example
The String.isEmpty method returns True if and only if the length of the user-specified string is Zero. Otherwise, it will return False. In this Java program, We are going to find the same.
//Java String.isEmpty Method example package StringFunctions; public class StringIsEmpty { public static void main(String[] args) { String str = "Tutorial GateWay"; String str1 =""; String str2 =" "; System.out.println("The Length of a String Str = " + str.length()); System.out.println("Is this String Str is Empty or Not? = " + str.isEmpty()); System.out.println("\nThe Length of a String Str1 = " + str1.length()); System.out.println("Is this String Str1 is Empty or Not? = " + str1.isEmpty()); System.out.println("\nThe Length of a String Str2 = " + str2.length()); System.out.println("Is this String Str2 is Empty or Not? = " + str2.isEmpty()); } }
From the following statements, the first Java statement will find the length of the above-specified string (which is 16). The second statement will call the public Boolean isEmpty () method to check whether the above-specified string is empty or not. As we all know, length = 16 (which is not equal to zero), so the output of Java String isEmpty will be False.
System.out.println("The Length of a String Str = " + str.length()); System.out.println("Is this String Str is Empty or Not? = " + str.isEmpty());
Here we declared the String variable and assigned an empty string using the following statement
String str1 ="";
From the following System.out.println statement will call the public Boolean isEmpty () method to check whether the above-specified string is empty or not. As we all know, length = 0 so the output will be True
System.out.println("Is this String Str1 is Empty or Not? = " + str1.isEmpty());
Next, we declared the String variable and assigned Empty space as a value using the following statement.
String str1 =" ";
From the above screenshot, you can observe that the String function is returning False. It is because the length of the above-specified string is 1. So Java isEmpty() method is returning False.
System.out.println("Is this String Str2 is Empty or Not? = " + str2.isEmpty());