Program to check if the String is Null in Java
Last Updated :
22 Jan, 2020
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:
class GFG {
public static boolean isStringNull(String str)
{
if (str == null )
return true ;
else
return false ;
}
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));
}
}
|
Output:
Is string [GeeksforGeeks] null? false
Is string [null] null? true
Share your thoughts in the comments
Please Login to comment...