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 Strings, Strings in JAVA, String Problems.
You can refer to the Data Structures and Algorithms, JavaScript 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!