Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Saturday, July 2, 2022

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

In Java, since null and empty are two different concepts, 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 to as a 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.

Btw, I am expecting that you are familiar with basic Java Programing and Java API in general. If you are a complete beginner then I suggest you first go through a comprehensive course like The Complete Java Masterclass on Udemy to learn more about core Java basics as well as such gems from Java API.

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

Here are my three solutions to this common problem. Each solution has its pros and cons and a special use case like 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 the below code that the first check is a 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 the 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 an 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 causes of NullPointerExcetpion in Java.

The second thing to keep in mind is that the isEmpty() method is available from Java SE 6 onwards, so this code will not work in Java 5 or the 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 the 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 an empty String. The emptiness check will fail because the 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 the 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 them to avoid pesky issues 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 just whitespaces as empty String in Java.

As suggested by others, if you are using Google Guava, Spring, or Apache commons then just check their StringUtils class, you might get a method that does this for you like 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 fundamentals 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 a character array better than a 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)
  • How to prepare for Java interviews? (guide and resources)


Unknown said...

we can also use StringUtils.isEmpty() method.

Anonymous 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.


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