Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Return Value
2.
Example 
2.1.
Output
3.
NULL vs Empty Strings
4.
Example(Checking NULL string using isEmpty())
4.1.
Output
5.
Example(Checking NULL string using ‘==’)
5.1.
Output
6.
Blank Strings
6.1.
Output
7.
FAQs
7.1.
What is isEmpty() function used for?
7.2.
What is the blank String?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

String isEmpty()

Author Nishant Rana
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Strings can be considered as the object or array of the sequence of characters. Strings in java are immutable which means that we can not directly update a string in java. Whenever we update a String, a new String is generated.
 

char a[] = {‘c’, ‘o’, ‘d’, ‘i’, ‘n’, ‘g’};
String s1 = new String(a);
String s2 = “coding”

 

Both s1 and s2 are “coding”.

isEmpty() function is basically used to check if the string is empty or not. A string is empty if it has no characters.

Return Value

It returns a boolean value:-

  1. True: If the string contains no character.
  2. False: If the string contains at least one character.

Example 

import java.util.*;


class Main {
  public static void main (String[] args) {
        String s1="";  
        String s2="CodingNinjas";  
        System.out.println(s1.isEmpty());  
        System.out.println(s2.isEmpty());  
  }
}

Output

true
false
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

NULL vs Empty Strings

In the starting, we said that the Empty Strings are the Strings that consist of zero or no characters. It is also the same for the NULL strings too, but we can not apply isEmpty() function on the NULL strings. For checking NULL strings, we need to compare the string with NULL using the ‘==’ operator.

Example(Checking NULL string using isEmpty())

import java.util.*;

class Main {
  public static void main (String[] args) {
        String str = null;  
        if(str.isEmpty())  
        {  
            System.out.println("The string is null.");  
        }  
        else  
        {  
            System.out.println("The string is not null.");  
        }
  }
}

Output

Exception in thread "main" java.lang.NullPointerException

at Main.mainMain.java:6)

Example(Checking NULL string using ‘==’)

import java.util.*;


class Main {
  public static void main (String[] args) {
        String str = null;  
        if(str == null)  
        {  
            System.out.println("The string is null.");  
        }  
        else  
        {  
            System.out.println("The string is not null.");  
        }
  }
}

Output

The string is null.

Blank Strings

Blank Strings are the strings that consist of the blank spaces only. For example, “   “. We can detect the blank strings using the trim() and the isEmpty() function of Strings. The trim() function removes all the blank space from the start and end of a String. After trimming the string we can apply the isEmpty() function.

import java.util.*;


class Main {
   public static void main (String[] args) {
        String str = "   ";  
        if(str.length() > 0 && str.trim().isEmpty())  
        {  
            System.out.println("The string is blank string.");  
        }  
        else  
        {  
            System.out.println("The string is not a blank string.");  
        }
   }
}

Output

The string is blank string.

FAQs

What is isEmpty() function used for?

The isEmpty() function is basically used to check if the string is empty or not. A string is empty if it has no characters.

What is the blank String?

Blank Strings are the strings that consist of the blank spaces only. For example, “   “.

Conclusion

In this article, we have extensively discussed the isEmpty() function of Strings in Java. We start with a brief introduction to Strings in Java, then discuss the isEmpty() function and its examples.

After reading this blog, you will be able to use isEmpty() function in Java, are you not feeling excited to read/explore more articles on the topic of file systems? Don't worry; Coding Ninjas has you covered. To learn, see StringsStrings in JAVAString Problems.

You can refer to the Data Structures and AlgorithmsJavaScript courses which can upskill you. You can discover more such courses by referring to the Guided Path on Coding Ninjas Studio. You can also test your knowledge by taking up the mock test and contests which are held by Code studio.

Check out this problem - Multiply Strings

If you are about to interview with companies like Google, Adobe, Amazon, etc. then you can refer to problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass