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

isEmpty

true

false

null

java.lang.NullPointerException

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 ; } } 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; } }

class has anmethod which returnsif the length of the string is 0 andif the string has at least one character.Before using this method, you should also check for the string not beingotherwise awill be thrown.

Above program prints

false

true

true

where false is when the string is not null and not empty.



Method 2 : Using Apache commons Library

org.apache.commons.lang3.StringUtils

isNotEmpty

true

null

null

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 ) ) ; } } 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)); } }

class hasclass having utility methods for string data type.This class has anmethod which takes a string argument and returnsif the supplied string argument is notand not empty.When using this method, there is no need to add an additionalcheck since this method handles null values itself.

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.



<!– 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’

For addingin your project, provide following dependencies as per the build tool.

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 ) ) ; } } 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.

1