Java Program to Check If a String is Empty or Null
In this tutorial, we will learn how to check if a string is empty or null. This can be done by various methods like if the length of the entered string is 0 then it is a null string. We can also use various pre-defined methods like equals(), isEmpty(), etc to check whether the string is empty or not. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.
Input: Enter the string: Apple
Output: The entered string is null or empty? : False
Program 1: Check If a String is Empty or Null
In this program, we will learn how to check if a string is empty or null using a relational operator.
Algorithm:
-
Start
-
Declare a string.
-
Initialize it with some values.
-
Use a relational operator to check whether the entered string is null or not.
-
Display the result.
-
Declare another string and initialize it to null.
-
Use a relational operator to check whether the entered string is null or not.
-
Display the result.
-
Stop.
The below example illustrates the implementation of the above algorithm.
/*Java Program to check if a string is empty or null*/
public class Main
{
public static void main(String[] args)
{
String str1 = "Study Tonight";
String str2 = null;
System.out.println("Is string: " + str1 +" empty or null? " + isEmptyOrNull(str1));
System.out.println("Is string: " + str2 + " empty or null? "+ isEmptyOrNull(str2));
}
public static boolean isEmptyOrNull(String str)
{
// use == relational operator and return the result
if (str == null)
return true;
else
return false;
}
}
Is string: Study Tonight empty or null? false
Is string: null empty or null? true
Program 2: Check If a String is Empty or Null
In this program, we will learn how to check if a string is empty or null using a relational operator or isEmpty().
Algorithm:
-
Start
-
Declare a string
-
Initialize it with some values.
-
Use a relational operator or isEmpty() to check whether the entered string is null or not.
-
Display the result.
-
Declare another string and initialize it to null.
-
Use a relational operator or isEmpty() to check whether the entered string is null or not.
-
Display the result.
-
Stop
The below example illustrates the implementation of the above algorithm.
/*Java Program to check if a string is empty or null*/
public class Main
{
public static void main(String[] args)
{
String str1 = "Study Tonight";
System.out.println("Entered String is: "+str1);
System.out.println("Is the entered string empty or null? "+str1 == null || str1.isEmpty()); //false
String str2 = "";
System.out.println("Entered String is: "+str2);
System.out.println("Is the entered string empty or null? "
+str2 == null || str2.isEmpty()); // true
}
}
Entered String is: Study Tonight
false
Entered String is:
true
Program 3: Check If a String is Empty or Null
In this program, we will learn how to check if a string is empty or null using the length() method. If length=0, then it is an empty or null string.
Algorithm:
-
Start
-
Declare a string
-
Initialize it with some values.
-
Use length() to check whether the entered string is null or not.
-
If the length of the entered string is 0 it is an empty string.
-
Display the result.
-
Declare another string and initialize it to null.
-
Use length() to check whether the entered string is null or not.
-
If the length of the entered string is 0 it is an empty string.
-
Display the result.
-
Stop
The below example illustrates the implementation of the above algorithm.
/*Java Program to check if a string is empty or null*/
public class Main
{
public static void main(String[] args)
{
String str1 = "Study Tonight";
System.out.println("Entered String is: "+str1);
System.out.println("Is the entered string empty or null? " +str1 == null || str1.length() == 0); //false
String str2 = "";
System.out.println("Entered String is: "+str2);
System.out.println("Is the entered string empty or null? "
+str2 == null || str2.length() == 0); // true
}
}
Entered String is: Study Tonight
false
Entered String is:
true
Program 4: Check If a String is Empty or Null
In this program, we will learn how to check if a string is empty or null. Here, we will use the .equals() method to do an equality check against an empty string.
Algorithm:
-
Start
-
Declare a string.
-
Initialize it with some values.
-
Use the equals() method to do an equality check against an empty string.
-
Display the result.
-
Declare another string and initialize it to null.
-
Use the equals() method to do an equality check against an empty string.
-
Display the result.
-
Stop
The below example illustrates the implementation of the above algorithm.
/*Java Program to check if a string is empty or null*/
public class Main
{
private static String EMPTY = "";
public static void main(String[] args)
{
String str1 = "Study Tonight";
System.out.println("Entered String is: "+str1);
System.out.println("Is the entered string empty or null? ");
System.out.println(str1 == null || EMPTY.equals(str1)); // false
System.out.println(str1 == null || str1.equals(EMPTY)); // false
String str2 = "";
System.out.println("Entered String is: "+str2);
System.out.println("Is the entered string empty or null? ");
System.out.println(str2 == null || EMPTY.equals(str2)); // true
System.out.println(str2 == null || str2.equals(EMPTY)); // true
}
}
Entered String is: Study Tonight
Is the entered string empty or null?
false
false
Entered String is:
Is the entered string empty or null?
true
true