Signup/Sign In

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:

  1. Start

  2. Declare a string.

  3. Initialize it with some values.

  4. Use a relational operator to check whether the entered string is null or not.

  5. Display the result.

  6. Declare another string and initialize it to null.

  7. Use a relational operator to check whether the entered string is null or not.

  8. Display the result.

  9. 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:

  1. Start

  2. Declare a string

  3. Initialize it with some values.

  4. Use a relational operator or isEmpty() to check whether the entered string is null or not.

  5. Display the result.

  6. Declare another string and initialize it to null.

  7. Use a relational operator or isEmpty() to check whether the entered string is null or not.

  8. Display the result.

  9. 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:

  1. Start

  2. Declare a string

  3. Initialize it with some values.

  4. Use length() to check whether the entered string is null or not.

  5. If the length of the entered string is 0 it is an empty string.

  6. Display the result.

  7. Declare another string and initialize it to null.

  8. Use length() to check whether the entered string is null or not.

  9. If the length of the entered string is 0 it is an empty string.

  10. Display the result.

  11. 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:

  1. Start

  2. Declare a string.

  3. Initialize it with some values.

  4. Use the equals() method to do an equality check against an empty string.

  5. Display the result.

  6. Declare another string and initialize it to null.

  7. Use the equals() method to do an equality check against an empty string.

  8. Display the result.

  9. 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



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.