Program to check if the String is Null in Java Last Updated : 27 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, checking if a string is null is essential for handling null-safe conditions and preventing runtime errors. To check if a string is null in Java, we can use the "==" operator that directly compares the string reference with null.Example:The below example demonstrates how to check if a given string is null using the == relational operator. Java // Java Program to check if // a String is Null class StringNull { // Method to check if the String is Null public static boolean isStringNull(String s) { // Compare the string with null // using == relational operator // and return the result if (s == null) return true; else return false; } // Driver code public static void main(String[] args) { // Test strings String s1 = "GeeksforGeeks"; String s2 = null; System.out.println("Is string [" + s1 + "] null? " + isStringNull(s1)); System.out.println("Is string [" + s2 + "] null? " + isStringNull(s2)); } } OutputIs string [GeeksforGeeks] null? false Is string [null] null? true Explanation: The method isStringNull uses the == operator to check if the input string is null. It returns a boolean result, which is displayed for both null and non-null strings. Comment More infoAdvertise with us Next Article Program to check if a String in Java contains only whitespaces C code_r Follow Improve Article Tags : Java Java-Strings Java-String-Programs Practice Tags : JavaJava-Strings Similar Reads Program to check if the String is Empty in Java Given a string str, the task is to check if this string is empty or not, in Java. Examples: Input: str = "" Output: True Input: str = "GFG" Output: False Approach 1: Get the String to be checked in strWe can simply check if the string is empty or not using the isEmpty() method of String class Syntax 2 min read Program to check if a String in Java contains only whitespaces Given a string str, the task is to check if this string contains only whitespaces or some text, in Java. Examples: Input: str = " " Output: True Input: str = "GFG" Output: False Approach: Get the String to be checked in str We can use the trim() method of String class to remove the leading whitespac 2 min read Null Pointer Exception in Java A NullPointerException in Java is a RuntimeException. It occurs when a program attempts to use an object reference that has the null value. In Java, "null" is a special value that can be assigned to object references to indicate the absence of a value.Reasons for Null Pointer ExceptionA NullPointerE 5 min read Java program to remove nulls from a List Container List is an ordered collection of objects which allows to store duplicate values or null values, in the insertion order. So it is very important to remove null values in many scenarios. Examples: Input: [Geeks, null, forGeeks, null, A computer portal] Output: [Geeks, forGeeks, A computer portal] Inpu 6 min read Check if a String Contains only Alphabets in Java using Regex Regular Expressions or Regex is an API for defining String patterns that can be used for searching, manipulating, and editing a text. It is widely used to define a constraint on strings such as a password. Regular Expressions are provided under java.util.regex package. For any string, here the task 2 min read Check if a String Contains only Alphabets in Java In Java, to check if a string contains only alphabets, we have to verify each character to make sure it falls within the range of valid alphabetic characters. There are various ways to check this in Java, depending on requirements.Example: The most common and straightforward approach to validate if 4 min read Like