Java String isEmpty()

24 Mar 2025 | 4 min read

The Java String class isEmpty() method checks if the input string is empty or not. Note that here empty means the number of characters contained in a string is zero.

The isEmpty() method in Java is a part of the String class and is used to check whether a string is empty or not. It returns a boolean value true if the string is empty (contains no characters), and false otherwise. An empty string is one that has a length of zero.

This method is useful when you need to determine if a string contains any meaningful content before performing certain operations on it. For example, you might use isEmpty() to check if a user input field is filled out before processing the input.

Signature

The signature or syntax of string isEmpty() method is given below:

Returns

true if length is 0 otherwise false.

Since

1.6

Internal implementation

Java String isEmpty() Method Example

Output:

true
false

Java String isEmpty() Method Example 2

Output:

String s1 is empty
TpointTech

Empty Vs. Null Strings

Earlier in this section, we have discussed that the empty strings contain zero characters. However, the same is true for a null string too. A null string is a string that has no value.

The isEmpty() method is not fit for checking the null strings. The following example shows the same.

Output:

Exception in thread "main" java.lang.NullPointerException
	at StringIsEmptyExample3.main(StringIsEmptyExample3.java:7)

Here, we can use the == operator to check for the null strings.

Output:

The string is null.

Let's understand in detail with the help of a Java example program.

Example 1:

Output:

str1 is empty: true
str2 is empty: true
str3 is empty: false
str4 is empty: false
str5 is empty: false
str6 is empty: false
str7 is empty: false
str8 is empty: false

Blank Strings

Blank strings are those strings that contain only white spaces. The isEmpty() method comes in very handy to check for the blank strings. Consider the following example.

Output:

The string is blank. 
The string is not blank.

It's important to note that isEmpty() is different from checking if a string is null. If a string variable is null, calling isEmpty() on it will result in a NullPointerException. To avoid this, it's a good practice to first check if the string is null before calling isEmpty().

In summary, the isEmpty() method in Java provides a simple and convenient way to check if a string contains any characters, helping to prevent errors when working with strings in your programs.