Java String is null or empty example shows how to check if the String is null or empty in Java. There are a couple of ways using which we can check the string.
How to check if the String is null or empty in Java?
Checking the null string is fairly simple. We need to compare the String reference with null using the == operator. Please see the below example that checks that.
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.javacodeexamples.stringexamples; public class StringIsNullOrEmpty { public static void main(String[] args) { String str1 = null; if(str1 == null) System.out.println("String is null"); } } |
Output
1 |
String is null |
Important Note: We cannot check if the string is null using the equals
or equalsIgnoreCase
methods. In case the string is indeed null, these methods will throw NullPointerException. Please see the below example.
1 2 3 4 |
String str1 = null; if(str1.equals(null)) System.out.println("String is null"); |
Output
1 2 |
Exception in thread "main" java.lang.NullPointerException at com.javacodeexamples.stringexamples.StringIsNullOrEmpty.main(StringIsNullOrEmpty.java:9) |
It throws this exception because, since the string reference is null, calling any methods on the null reference throws the NullPointerException.
Checking for an empty string can be done using several ways. We can use the length
method of the string, we can use the isEmpty
method, or we can use the equals/equalsIgnoreCase methods.
The String length
method returns an int value representing the number of characters it contains. We can compare this with 0 to check if the string is empty.
1 2 3 4 |
String str1 = ""; if( str1.length() == 0 ) System.out.println("String is empty"); |
Output
1 |
String is empty |
We can also use the isEmpty
method to check the same. This method returns a boolean value, true if the string is empty, false otherwise.
1 2 3 4 |
String str1 = ""; if( str1.isEmpty() ) System.out.println("String is empty"); |
Output
1 |
String is empty |
The isEmpty
method was introduced in Java 1.6 and internally calls the length
method to check. It is recommended to use the isEmpty
method instead of the length
method to check if the string is empty.
Now to check both, null and empty, we need to combine both of these checks using the OR operator “||” as given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
String str1 = ""; String str2 = null; String str3 = "abc"; if( str1 == null || str1.isEmpty() ) System.out.println("String1 is null or empty"); if( str2 == null || str2.isEmpty() ) System.out.println("String2 is null or empty"); if( str3 == null || str3.isEmpty() ) System.out.println("String3 is null or empty"); |
Output
1 2 |
String1 is null or empty String2 is null or empty |
Important Note: The order of the null and empty checks is important here. We cannot first check for the empty and then null value because then if the string is null, it will throw NullPointerException.
Lastly, if you are using the Guava library, you can use the isNollOrEmpty
method of the Strings class to check. It returns true if the string is null or empty, false otherwise.
Similarly, if you are using the Apache commons library, you can use the isEmpty
method of the StringUtils class to check for the empty string.
If you want to learn more about the strings, please visit the Java String Tutorial.
Please let me know your views in the comments section below.