In this tutorial, you will learn about String isEmpty() method, its syntax, and usage with examples.

Java String isEmpty() method

In Java, String isEmpty() method is used to check if the string is empty or not. The method returns true if the string is empty, or else it returns false.

Syntax of isEmpty()

The syntax to call String isEmpty() method in Java is

string.isEmpty()

isEmpty() method takes no parameters.

isEmpty() returns value of boolean type. As already mentioned, the method returns true is the string is empty, or returns false if the string is not empty.

ADVERTISEMENT

Examples

1. isEmpty() – Check if the string “” is empty in Java

In this example, we take an empty string value in str and programmatically check if the string is empty using String isEmpty() method. We will use the value returned by the method as a condition in the Java if else statement.

Java Program

public class Main {
	public static void main(String[] args) {
		String str = "";
		if (str.isEmpty()) {
			System.out.println("Given string is EMPTY.");
		} else {
			System.out.println("Given string is NOT EMPTY.");
		}
	}
}

Output

Given string is EMPTY.

2. isEmpty() – Check if the string “Hello World” is empty in Java

In this example, we take a string value "Hello World" in str variable and programmatically check if the string is empty using String isEmpty() method.

Java Program

public class Main {
	public static void main(String[] args) {
		String str = "Hello World";
		if (str.isEmpty()) {
			System.out.println("Given string is EMPTY.");
		} else {
			System.out.println("Given string is NOT EMPTY.");
		}
	}
}

Output

Given string is NOT EMPTY.

Conclusion

In this Java String Methods tutorial, we have seen about String isEmpty() method in Java, its syntax, and how to use String isEmpty() method in Java programs with the help of examples.