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 = "GeeksforGeeks"; String str2 = null; System.out.println("Is string [" + str1 + "] null? " + isStringNull(str1)); System.out.println("Is string [" + str2 + "] null? " + isStringNull(str2)); } } |
chevron_right
filter_none
Output:
Is string [GeeksforGeeks] null? false Is string [null] null? true
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Method overloading and null error in Java
- Interesting facts about null in Java
- Null Pointer Exception In Java
- Calling a method using null in Java
- Replace null values with default value in Java Map
- Null Cipher
- Java program to check whether a string is a Palindrome
- Program to check if the String is Empty in Java
- Program to check if a String in Java contains only whitespaces
- C Program to Check if a Given String is Palindrome
- Python program to check if a string is palindrome or not
- Program to check if input is an integer or a string
- Program to check if first and the last characters of string are equal
- Python program to check if a string contains all unique characters
- Python program to check if given string is vowel Palindrome
- Python program to check if given string is pangram
- C++ program to check whether a String is a Pangram or not
- Check if a string can become empty by recursively deleting a given sub-string
- Check if a string can be obtained by rotating another string 2 places
- String slicing in Python to check if a string can become empty by recursive deletion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

