Home » Java String isEmpty() method

Java String isEmpty() method

by Online Tutorials Library

Java String isEmpty()

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.

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

FileName: StringIsEmptyExample.java

Test it Now

Output:

true  false  

Java String isEmpty() Method Example 2

FileName: StringIsEmptyExample2.java

Output:

String s1 is empty TutorAspire 

Empty Vs. Null Strings

Earlier in this tutorial, 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.

FileName: StringIsEmptyExample3.java

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.

FileName: StringIsEmptyExample4.java

Output:

The string is null.  

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.

FileName: StringIsEmptyExample5.java

Output:

The string is blank.   The string is not blank.  

Next TopicJava String join()

You may also like