Thursday, September 4, 2025
HomeLanguagesJavaProgram to check if the String is Null in Java

Program to check if the String is Null in Java

Given a string str, the task is to check if this string is null or not, in Java.

Examples:

Input: str = null 
Output: True

Input: str = "GFG"
Output: False

Approach:

  • Get the String to be checked in str
  • We can simply compare the string with Null using == relational operator.
    Syntax:
    if(str == null)
    
  • Print true if the above condition is true. Else print false.

Below is the implementation of the above approach:




// Java Program to check if
// the String is Null in Java
  
class GFG {
  
    // Function to check if the String is Null
    public static boolean isStringNull(String str)
    {
  
        // Compare the string with null
        // using == relational operator
        // and return the result
        if (str == null)
            return true;
        else
            return false;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String str1 = "Lazyroar";
        String str2 = null;
  
        System.out.println("Is string [" + str1
                           + "] null? "
                           + isStringNull(str1));
        System.out.println("Is string [" + str2
                           + "] null? "
                           + isStringNull(str2));
    }
}


Output:

Is string [Lazyroar] null? false
Is string [null] null? true
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS