String isEmpty() method - Syntax and Examples
Learn how to use the Java String isEmpty() method to check if a string is empty. Includes syntax, parameters, return value, and practical examples.
This reference explains String isEmpty() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
Introduction
Sometimes you need to know if a string contains any characters or not. The isEmpty() method in Java provides a simple and direct way to determine whether a String object is empty.
Syntax
public boolean isEmpty()
Parameters
| Parameter | Description |
|---|---|
| None | This method doesn't accept any parameters. |
Return Value
The isEmpty() method returns a boolean value:
true: If the string has length 0 (it's empty).false: If the string has any characters.
Examples
Example 1: Checking an Empty String
This example demonstrates how to use isEmpty() with a truly empty string.
public class Main {
public static void main(String[] args) {
String str = ""; // An empty string
boolean isEmpty = str.isEmpty();
System.out.println("Is the string empty? " + isEmpty);
}
}
Is the string empty? true
Here, we create a String variable str and initialize it with an empty string (""). We then call isEmpty() on this string. Since the string is empty, isEmpty() returns true.
Example 2: Checking a Non-Empty String
This example illustrates how isEmpty() behaves with a string that contains characters.
public class Main {
public static void main(String[] args) {
String str = "Hello"; // A non-empty string
boolean isEmpty = str.isEmpty();
System.out.println("Is the string empty? " + isEmpty);
}
}
Is the string empty? false
In this case, str is initialized with the value “Hello”. Since the string has characters, isEmpty() returns false.
Example 3: Checking a String Containing Only Whitespace
This example demonstrates that whitespace-only strings are *not* considered empty by isEmpty(). They have length greater than zero.
public class Main {
public static void main(String[] args) {
String str = " "; // A string containing only whitespace
boolean isEmpty = str.isEmpty();
System.out.println("Is the string empty? " + isEmpty);
}
}
Is the string empty? false
Even though str contains only spaces, it's not considered an empty string because its length is greater than zero. Therefore, isEmpty() returns false.
Example 4: Using isEmpty() for Validation
This example shows how you might use isEmpty() to validate user input before processing it.
public class Main {
public static void main(String[] args) {
String userInput = null;
if (userInput == null || userInput.isEmpty()) {
System.out.println("User input is empty or null. Please provide some data.");
} else {
System.out.println("Processing user input: " + userInput);
}
}
}
User input is empty or null. Please provide some data.
Here, we check if the userInput variable is either null (meaning it hasn't been assigned a value) or empty using isEmpty(). If either condition is true, we display an error message to the user. Otherwise, we proceed with processing the input.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.