How to check if String is not null and empty in Java?

In Java, since null and empty are two different concept, it's a little bit tricky for beginners to check if a String is both not null and not empty. A String reference variable points to null if it has not been initialized and an empty String is a String without any character or a String of zero length. Remember, a String with just whitespace may not be considered as empty String by one program but considered as empty String by others, so, depending upon your situation, you can include the logic to check for that as well. A String with just white space is also referred as blank String in java. In this tutorial, I will teach you a couple of right ways to test if a String is not null and not empty in Java.


3 Ways to check if String is null or empty in Java

Here are my three solutions of this common problem. Each solution has their pros and cons and a special use case e.g. the first solution can only be used from JDK 7 onward, the second is the fastest way to check if String is empty and the third solution should be used if your String contains whitespaces.



1st solution - using isEmpty() method
This is the most readable way to check for both whether String is null or not and whether String is empty or not. You can see from below code that first check is null check and the second check is for emptiness.

if(stirng != null && !string.isEmpty()){
   System.out.println("String is not null and not empty");
}

A couple of things to remember about this solution is that you must keep the order same because isEmpty() is a non-static method and if called on the null reference it will throw NullPointerException. Since we are first doing a null check and then empty check using the && operator, which is a short circuit AND operator. This operator will not check for emptiness if String is null hence no NPE. This is also a good trick to avoid NPE in Java.


Btw, you must be careful with the order you carry the null and emptiness check. For example, if you reverse the order of checks i.e. first call the isEmtpy() method and then do the null check, you will get the NullPointerException. In fact, it is one of the most common cause of NullPointerExcetpion in Java.

Second thing to keep in mind is that isEmpty() method is available from Java SE 6 onwards, so this code will not work in Java 5 or lower version. There you can use the length() method to check emptiness, as shown in the second example.

How to check if String is not null and not empty in Java


2nd solution - Using length() function
This is the universal solution and works in all versions of Java, from JDK 1.0 to Java 8. I highly recommend this method because of portability advantage it provides. It is also the fastest way to check if String is empty in Java or not.

if(stirng != null && string.length() > 0){
   System.out.println("String is not null and not empty");
}

One thing which is very important here is that if String contains just white space then this solution will not consider it as empty String. The emptiness check will fail because string.length() will return a non-zero value. If you are considering the String with only whitespaces as empty then use the trim() method as shown in the third example.



3rd solution - Using trim() method
This is the third way to check whether String is empty or not. This solution first calls the trim() method on the String object to remove the leading and trailing white spaces.

if(stirng != null && string.trim().length() > 0){
   System.out.println("String is not null and not empty");
}

You can use this solution if your program doesn't consider a String with only white-space as a non-empty. If you load data from the database or stored data into the database it's better to trim() them before using to avoid pesky issue due to whitespaces.


That's all about how to check if String is not null and not empty in Java. You can use any of the above three methods but you must remember the pros and cons of each method. Sometimes you need to use the trim() method, if your program's requirement doesn't consider a String with the only whitespace as non-empty, but other times, you might want to use the length() function to consider String with justs whitespaces as empty String in Java.

As suggested by others, if you are using Google Guava, Spring or Apache commons than just check their StringUtils class, you might get a method which does this for you e.g. StringUtils.isBlank() from Apache Commons.  Remember, even Joshua Bloch has advised in Effective Java to learn and use library functions, whenever possible, but only if you understand the fundamental behind it.


Further Reading
  • When to use the intern() method of String in Java? (answer)
  • Why is String Immutable and final in Java?  (answer)
  • Why is character array better than String for storing the password in Java? (reason)
  • How substring() method works in Java 6? (answer)
  • The difference between String literal and new() String object in Java? (answer)
  • Java ProgrammingInterview Questions exposed (book)

6 comments :

obul subbareddy said...

we can also use StringUtils.isEmpty() method.

Chris FAKAR said...

There is a typo in this sentence:

"Remember, a String full of white character may not be considered empty but some but may be considered empty by others, so depending upon your situation, you can include logic to check for that as well."

Besides that, great hint for beginners.

SARAL SAXENA said...

I like to use Apache commons-lang for these kinds of things, and especially the StringUtils utility class:

import org.apache.commons.lang.StringUtils;

if (StringUtils.isNotBlank(str)) {
...
}

if (StringUtils.isBlank(str)) {
...
}

Aulo said...

For readability and failsafe code, definitely also recommending the Apache commons-lang StringUtils for such checks. It just works :)

Prithviraj said...

I prefer apache commons utility.
StringUtils.isEmpty() and StringUtils.isBlank()

Javin Paul said...

Thanks for your comments Guys, I agree using a library function such as Apache Commons isBlank() is the right way to do null and empty check and everybody should do that in production only if that library is already in classpath. Though, It's important for Java beginners to know how they can do this in JDK as well.

Post a Comment