Java String isEmpty Example
When we talk about Strings in Java, we can imagine them as arrays of characters.
An empty Java String, is considered as the not null String that contains zero charachers, meaning its length is 0. However, a Java String that might only contain the whitespace character is not considered as empty, it is considered to contain one character and its length is equal to 1.
There are several ways, that we can check if a Java String is empty. Some of the most popular and useful ways are:
- Checking if String is empty by using
isEmpty()
String method. - Checking if String is empty by using
isBlank()
String method. - Checking if
length()
String method for a String is equal to zero. - Checking if
equals()
String method returns true when we compare the given String with an empty one. - Using Spring framework’s
StringUtils.hasLength()
method.
String isEmpty() method:
So, in this example, we are going to see how to use the String isEmpty()
method. The static isEmpty()
method of the java.lang.String class returns a boolean which shows if the String length is 0, or not. If the length is 0 (zero), it will return true, otherwise the method will return false. However, when we want to check if a String is empty with the call of isEmpty()
method, we should in the first place check whether the Java String is null or not. If we try to call a static String method from an empty or null array, it may result to a java.lang.NullPointerException
. This is the reason that we might first try to ensure that the Java String is initialized.
Example:
JavaStringIsEmpty.java
package com.javacodegeeks.javabasics.string; public class JavaStringIsEmpty { public static void main(String[] args) { String str1 = "JavaCodeGeeks isEmpty Example"; String str2 = ""; String str3 = " "; //this String is not considered as empty boolean check1 = str1.isEmpty(); boolean check2 = str2.isEmpty(); boolean check3 = str3.isEmpty(); System.out.println("Is str1 empty? The answer is: "+str1.isEmpty()); //checking if str1 is empty directly System.out.println("Is str1 empty? The answer is: "+check1); //checking if str1 is empty with the help of check1 boolean System.out.println("Is str2 empty? The answer is: "+str2.isEmpty()); //checking if str2 is empty directly System.out.println("Is str2 empty? The answer is: "+check2); //checking if str2 is empty with the help of check2 boolean System.out.println("Is str3 empty? The answer is: "+str3.isEmpty()); //checking if str3 is empty directly System.out.println("Is str3 empty? The answer is: "+str3.trim().isEmpty()); //checking if str3 is empty after trimming the String of any whitespaces } }
Output:
Is str1 empty? The answer is: false
Is str1 empty? The answer is: false
Is str2 empty? The answer is: true
Is str2 empty? The answer is: true
Is str3 empty? The answer is: false
Is str3 empty? The answer is: true
This was the Java String isEmpty example. You can learn more about checking whether a String is empty in the example Check empty string.