Java String isEmpty Method

The Java isEmpty method is one of the 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 isEmpty will return TRUE.

In this article, we will show how to find Empty strings using a Java isEmpty method with an example. The basic syntax of the String isEmpty, 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 Java 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.

First, we declared three String variables, and we assigned some text for str. For the remaining two, we assigned an empty space or whitespace.

From the following statements, the first Java System.out.println statement will find the length of the above-specified str (which is 16).

The second statement will call the isEmpty() method to check whether the above-specified str 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.

The third System.out.println statement will check whether the above-specified str1 is empty or not. As we all know, length = 0, so the output will be True.

From the below screenshot, you can observe that the String function is returning False for str2. It is because the length of the above-specified str2 is 1. So the method is returning False.

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());
	}
}
Java String isEmpty Method 1