Skip to content
Related Articles

Related Articles

Improve Article

Program to check if the String is Null in Java

  • Difficulty Level : Basic
  • Last Updated : 22 Jan, 2020
Geek Week

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


Input: str = null 
Output: True

Input: str = "GFG"
Output: False


  • Get the String to be checked in str
  • We can simply compare the string with Null using == relational operator.
    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;
            return false;
    // Driver code
    public static void main(String[] args)
        String str1 = "GeeksforGeeks";
        String str2 = null;
        System.out.println("Is string [" + str1
                           + "] null? "
                           + isStringNull(str1));
        System.out.println("Is string [" + str2
                           + "] null? "
                           + isStringNull(str2));
Is string [GeeksforGeeks] null? false
Is string [null] null? true

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up
Recommended Articles
Page :