Site icon codippa

How to check if a string is null or empty in java / 3 ways to check for a null or empty string in java

String is the most commonly used data type in java. Often you need to perform operations on a string but before any operation it is necessary to check for an empty(or zero length) string and also whether the string is null to avoid java.lang.NullPointerException.

To know more about the most commonly faced NullPointerException, refer this article.

This post will explain different methods in which a string can be tested for being empty and null.
Method 1: Using isEmpty method
java.lang.String class has an isEmpty method which returns true if the length of the string is 0 and false if the string has at least one character.
Before using this method, you should also check for the string not being null otherwise a java.lang.NullPointerException will be thrown.
Example,

public class EmptyOrNullStringChecker {
   
   public static void main(String[] args) {
        // initialize string variables
	String nonEmptyString = "codippa.com";
	String nullString = null;
	String emptyString = "";
	System.out.println(isStringNullOrEmpty(nonEmptyString));
	System.out.println(isStringNullOrEmpty(nullString));
	System.out.println(isStringNullOrEmpty(emptyString));
   }

   private static boolean isStringNullOrEmpty(String stringToCheck) {
	// string is non-null and non-empty
	if (stringToCheck != null && !stringToCheck.isEmpty()) {
	   return false;
	}
	return true;
   }
}

Above program prints

false
true
true

where false is when the string is not null and not empty.
Method 2: Using Apache commons Library
Apache commons library class has org.apache.commons.lang3.StringUtils class having utility methods for string data type.
This class has an isNotEmpty method which takes a string argument and returns true if the supplied string argument is not null and not empty.
When using this method, there is no need to add an additional null check since this method handles null values itself.
Example,

import org.apache.commons.lang3.StringUtils;

public class EmptyOrNullStringChecker {
   
   public static void main(String[] args) {
        // initialize string variables
	String nonEmptyString = "codippa.com";
	String nullString = null;
	String emptyString = "";
	System.out.println(StringUtils.isNotEmpty(nonEmptyString));
	System.out.println(StringUtils.isNotEmpty(nullString));
	System.out.println(StringUtils.isNotEmpty(emptyString));
   }
}

Output of the program is

true
false
false

StringUtils class also has an isEmpty method which is the opposite of isNotEmpty. It returns true if the supplied string argument is null or empty, false otherwise.
For adding Apache commons library in your project, provide following dependencies as per the build tool.

<!– Maven –>
<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.9</version>
</dependency>

// Gradle
compile group: ‘org.apache.commons’, name: ‘commons-lang3’, version: ‘3.9’

Method 3: Using Guava library
Guava library has a com.google.common.base.Strings class which also has utility methods for string operations. It has an isNullOrEmpty method which takes a string argument.
As its name suggests, this method returns true if the supplied string argument is null or empty, false otherwise.
Example,

import com.google.common.base.Strings;

public class EmptyOrNullStringChecker {
   
   public static void main(String[] args) {
        // initialize string variables
	String nonEmptyString = "codippa.com";
	String nullString = null;
	String emptyString = "";
	System.out.println(Strings.isNullOrEmpty(nonEmptyString));
	System.out.println(Strings.isNullOrEmpty(nullString));
	System.out.println(Strings.isNullOrEmpty(emptyString));
   }
}

Above code prints

false
true
true

For adding Guava library in your project, provide following dependencies as per the build tool.

<!– Maven  –>
<dependency>
   <groupId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>28.0-jre</version>
</dependency>

// Gradle
compile group: ‘com.google.guava’, name: ‘guava’, version: ‘28.0-jre’

Hope you liked this post and learnt something from it. If yes, then hit the clap button below to show your appreciation.

Exit mobile version