Program to check if the String is Null in Java Last Updated : 27 Nov, 2024 Comments Improve Suggest changes 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 the String is Null in Java C code_r Follow Improve Article Tags : Strings Java Java Programs DSA Java-Strings Java-String-Programs +2 More Practice Tags : JavaJava-StringsStrings Similar Reads Java Program to Check if the TreeMap is Empty The TreeMap in Java is used to implement Map interface and NavigableMap along with the AbstractMap Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. Approaches: Using the isEmpty() methodU 3 min read Java Program to Convert Boolean to String In Java programming, you might come across the need to convert a simple "true" or "false" boolean value into a String. It may seem like a challenging task, but fear not! In this article, You will explore some methods to convert a boolean value to a string in Java Method for Boolean to String Convers 4 min read Java Program to Check if a Directory is Empty or Not The list() method defined in class java.io.File which is used to obtain the list of files and folders(directories) present in the specified directory defined by its pathname. The list of files is stored in an array of string. If the length of an array is greater than 0, then the specified directory 3 min read How to Check if a String Contains only Digits in Java? In Java, to check if a string contains only digits, we can use various methods such as simple iteration, regular expressions, streams, etc.Example:The example below uses the Character.isDigit() method to check each character of the string to ensure all characters are digits. This is the most simple 3 min read How to Check if LinkedHashMap is Empty in Java? The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements 2 min read How to Check if a PriorityQueue is Empty or Contains Elements in Java? In this article, we will demonstrate how to check if a Priority Queue is empty or not. The java.util.PriorityQueue.isEmpty() method is used to check if the Priority Queue is empty or not. Based on the Boolean value it returns the Response for the same. Syntax: Priority_Queue.isEmpty()Parameters: The 2 min read Check if a String Contains Only Alphabets in Java using ASCII Values Given a string, now we all know that the task is to check whether a string contains only alphabets. Now we will be iterating character by character and checking the corresponding ASCII value attached to it. If not found means there is some other character other than "a-z" or "A-Z". If we traverse th 4 min read Check if Particular Value Exists in Java HashMap Java HashMap is an implementation of the Map interface which maps a Value to a Key which essentially forms an associative pair wherein we can call a Value based on the Key. Java HashMap provides a lot of advantages such as allowing different data types for the Key and Value which makes this data str 5 min read Java String equals() Method String equals() method in Java compares the content of two strings. It compares the value's character by character, irrespective of whether two strings are stored in the same memory location. The String equals() method overrides the equals() method of the object class. false if any of the characters 2 min read How to Handle NULL Values in JDBC? Java programming can be able to handle databases by using JDBC API. In this article, we will discuss how to handle NULL values in JDBC. For this problem, we have inserted some data into an already created table named book in my Database named books. To handle null values in JDBC involves checking fo 4 min read Like