Table of contents
1.
Introduction
2.
What is StringUtils.isEmpty in Java?
2.1.
How to Import StringUtils
2.1.1.
Gradle
2.1.2.
Maven
2.2.
Example 
2.3.
StringUtils isEmpty() Example in Java
2.3.1.
Output
2.3.2.
Explanation
3.
Frequently Asked Questions
3.1.
How to check if StringUtils is empty?
3.2.
What is the difference between isBlank and isEmpty in StringUtils?
3.3.
What is StringUtils default if empty?
3.4.
Why use isEmpty () in Java?
3.5.
What is null isEmpty () in Java?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

StringUtils IsEmpty

Author Sanchit Kumar
0 upvote

Introduction

When we deal with StringUtils class in java, we come across various methods. Such as "Trim", "Split", "Remove", "Replace", "IsEmpty", etc. Here in this article, we will learn about the "IsEmpty" method of StringUtils class in java. Cool! Are you wondering about StringUtils IsEmpty and how to use it?

StringUtils IsEmpty

Fine! So let us learn about StringUtils IsEmpty.

Recommended topic -  Iteration Statements in Java, and Duck Number in Java.

What is StringUtils.isEmpty in Java?

StringUtils.isEmpty() is a utility method in the Apache Commons Java library that determines whether a provided String is null or empty. (has zero length). If the String is null or has no length, it returns true; otherwise, it returns false.


Syntax - public static boolean isEmpty(CharSequence XYZ).

In the syntax, the parameter is "XYZ", the CharSequence to check. If the CharSequence is empty or null, it will return true; otherwise, it will return false.


Note -  Lang version 3.0 changed signature from isEmpty(String) to isEmpty(CharSequence).

How to Import StringUtils

We need the "StringUtils" class from "Apache Commons Lang3" before we move on and use the "IsEmpty" method. It provides utility methods for manipulating and checking strings.

First, you need to import the jar file(commons-lang3-3.12.0.jar) into our project. You can import through a dependency management tool like "Gradle" or "Maven".

Gradle

Add the following line in the "build.gradle" file.

build.gradle

compile 'org.apache.commons:commons-lang3:3.12.0'


Note -  The "build.gradle" is Gradle's default build script name.

Maven

Add the following line in the "pom.xml" file.

pom.xml

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.12.0</version>
</dependency>


Note

The "pom.xml" is Maven's default build script name.


Now, write the following line to import the StringUtils class.

import org.apache.commons.lang3.StringUtils;

Example 

Statement

Result

StringUtils.isEmpty(null)

true

StringUtils.isEmpty("")

true

StringUtils.isEmpty(" ")

false

StringUtils.isEmpty("characters")

false

StringUtils.isEmpty("  characters  ")

false

Related Article Apache Server and Hashcode Method in Java.

StringUtils isEmpty() Example in Java

import org.apache.commons.lang3.StringUtils;
/*Make sure to add Apache Commons Lang jar files before importing StringUtils*/

public class StringUtilsIsEmpty {
	public static void main(String[] args) {
		String str1 = "";
		String str2 = " ";
		String str3 = null;
		String str4 = "something";
		System.out.println(StringUtils.isEmpty(str1));
		System.out.println(StringUtils.isEmpty(str2));
		System.out.println(StringUtils.isEmpty(str3));
		System.out.println(StringUtils.isEmpty(str4));
	}
}
You can also try this code with Online Java Compiler
Run Code


Output

true
false
true
false


Explanation

  • str1 = "" - The length of the string is zero, so the method returns true.
  • str2 = " " - The string is not null with a non-zero string length, so the method returns true.
  • str3 = null - The string points to a null reference, so the method returns true.
  • str4 = "something" - The string is not null, and the length of the string is greater than zero, so the method returns true.


Remember - The isEmpty() method does not check for whitespace. It returns false if the string is whitespace.


We hope you understand StringUtils IsEmpty(), and you can do this code by yourself on Online Java Compiler.

Must Read Conditional Statements in Java

Frequently Asked Questions

How to check if StringUtils is empty?

To check if the string is empty or not in StringUtils, you can use isEmpty() method in java, and if the length of the string is zero or the string points to a null reference, then the string is empty.

What is the difference between isBlank and isEmpty in StringUtils?

The main difference between isBlank and isEmpty in StringUtils is that isBlank checks whether the string is empty or contains whitespaces like spaces or line break, whereas in isEmpty it only checks whether the string is empty or not.

What is StringUtils default if empty?

The StringUtils defaultIfEmpty() static method in Java. It works on two parameters, string and default string. The default string is returned if the provided string is null or empty. Otherwise, the string is returned.

Why use isEmpty () in Java?

The isEmpty function in java checks whether the string is empty. This method validates the user input and avoids null pointer exceptions. It is useful to avoid unnecessary computations or operations on an empty string.

What is null isEmpty () in Java?

The isEmpty() function in java determines whether or not a string is empty. This method returns true if the string is empty (length() is 0). Otherwise, it returns false.

Conclusion

This article discussed StringUtils IsEmpty. We learnt the IsEmpty method of StringUtils class and how to use the IsEmpty method to check if a CharSequence is empty or null.

We hope this blog on StringUtils IsEmpty was helpful. You can refer to other similar articles as well - 

Recommended problems -


Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, System Design, Competitive Programming, etc. Enrol in our courses and refer to the problems available and mock tests. Take a look at the interview bundle and interview experiences for placement preparations.

Happy Learning Ninja! 🥷

Live masterclass