String is a commonly used data type in Java that stores the data in the form of text or characters. Checking if the given string is null or not is a common task that Java developers perform oftentimes. For example, for validating user input, for better file processing (while reading data from them), while interacting with databases, and so on. Therefore, it’s essential to have a reliable method for checking whether a string is null or empty before proceeding with operations. Thankfully, Java provides several methods that serve this purpose effectively. This Java guide illustrates these methods with suitable examples.
How to Check if a String is Null in Java Using Relational Operator “==”
In Java, the Relational Operator “==” is used to check if the given string is null or not. You can use this operator with the help of if-else statements to check if the given string is null or not and return the result accordingly.
Example 1: How to Check if a String is Null
For example, in the following code, we create a function isNull() that will check if the provided string is null or not. If yes, it will return a message “Given String is Null”. If the string is not null it will show the length of the given string:
public class CheckNullStrings {
public static void isNull(String string) {
if (string == null)
{
System.out.println("The Given String(" + string +") is Null");
}
else
{
System.out.println("Length of String '" + string + "' is " + string.length());
}
}
public static void main(String[] args) {
String inputStr1 = null;
String inputStr2 = "Welcome to JavaBeat";
String inputStr3 = " ";
isNull(inputStr1);
isNull(inputStr2);
isNull(inputStr3);
}
}
In the main() method, we declare and initialize three strings: inputStr1, inputStr2, and inputStr3. The first string is null, the second one contains some characters, while the third one is a collection of whitespaces. We invoke the “isNull()” function on each of the given strings and we get the following result on the console:
The output shows that the “==” operator treats the “null” and an empty string differently (i.e., null and “ ” are not equal).
Example 2: How to Check if a String is Null or Blank/Empty
A string is referred to as a blank string if it is empty or contains only whitespaces. You can apply the “isBlank()” method on any given string to check if it is blank or not. It retrieves true if the given string is empty or contains some whitespaces:
public class CheckNullStrings {
public static void isNull(String string) {
if (string == null) {
System.out.println("The Given String(" + string + ") is Null");
} else if (string.isBlank()) {
System.out.println("The Given String(" + string + ") is Blank");
} else {
System.out.println("Length of String '" + string + "' is " + string.length());
}
}
public static void main(String[] args) {
String givenStr1 = null;
String givenStr2 = "Welcome to JavaBeat";
String givenStr3 = "";
isNull(givenStr1);
isNull(givenStr2);
isNull(givenStr3);
}
}
In this example, all the code is the same as in example 1. The only difference is that this time we use the “isBlank()” method within the “else if” condition to check if the given string is blank. Here is what we get on successful execution of the code:
Important: You can also use the “isEmpty()” method to check if the given string is empty or not. However, this method will retrieve true only if the provided string is empty, i.e., “”. It retrieves false if the targeted string contains some whitespaces.
Example 3: How to Fix NullPointerException
If you apply the “isBlank()” or “isEmpty()” method on a null string you will encounter a “NullPointerException”. This exception states that you can’t invoke the stated methods on a null string:
To fix this error, it is recommended to use the isEmpty() or isBlank() methods of the Apache Common Lang library. The isEmpty() and isBlank() methods of this library are flexible and can deal with the null strings efficiently. However to use these methods, first, you need to create a maven project and add the below-stated dependency in its auto-created “.xml” file:
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
</dependencies>
Now you are all set to use any method and class of the Apache Commons library:
package mavenExample;
import org.apache.commons.lang3.StringUtils;
public class CheckNullString {
public static void main(String args[]) {
String inputStr1 = null;
String inputStr2 = "Welcome to JavaBeat";
String inputStr3 = "";
System.out.println(inputStr1 + " is empty: " + StringUtils.isBlank(inputStr1));
System.out.println(inputStr2 + " is empty: " + StringUtils.isBlank(inputStr2));
System.out.println(inputStr3 + " is empty: " + StringUtils.isBlank(inputStr3));
}
}
In this code, we create three strings: inputStr1, inputStr2, and inputStr3. We initialize these strings with “null”, “Welcome to JavaBeat”, and “”, respectively. We invoke the “StringUtils.isBlank()” method on all the given strings, as a result, we observed that the stated method successfully retrieves the results (without causing any “NullPointerException”:
How to Check if a String is Null in Java Using isNull()
The “isNull()” is a built-in method of the Objects class that accepts a string and checks if it is null or not. To use this method, first, you must import the Objects class in your code and then you can utilize this method as per your needs:
package exp;
import java.util.Objects;
public class CheckNullStrings {
public static void isNullString(String string) {
if (Objects.isNull(string)) {
System.out.println("The Given String is Null");
} else {
System.out.println("The Length of Given String is: " + string.length());
}
}
public static void main(String[] args) {
String inputStr1 = null;
String inputStr2 = "JavaBeat.net";
isNullString(inputStr1);
isNullString(inputStr2);
}
}
In this example, first, we import the Objects class from the “java.util” package. After this, we create a user-defined function “isNullString()” that accepts a string as an argument. Within this function, we use the “isNull()” method to check if the provided string is null or not. In the main() method, we declare and initialize a couple of strings. Finally, we invoke the user-defined “isNullString()” method on the given strings to check if they are null or not:
This is how you can check if the given string is null or not in Java.
Frequently Asked Questions
This section addresses some commonly asked queries (regarding null strings) by Java users on different forums/platforms:
Can We Use the equals() Method to Check if a String is Null or Not
The equals() method throws a NullPointerException if the provided string is “null”.
Are Empty, Null, and Blank Strings the Same in Java
No, an empty string is a string having length 0, a blank string is a string that contains one or more whitespaces (only), while a null string has no value(at all).
isEmpty() Vs isBlank() – What’s the Difference
The isEmpty() method retrieves true if and only if the provided string has no character(length/size 0). On the other hand, the isBlank() method retrieves “true” if the given string contains one or more whitespaces.
Conclusion
To check if a string is Null, you can use Java’s relational operator “==” or the “Objects.isNull()” method. Both these approaches return a Boolean value: true or false. You can implement the “==” operator with the help of an if-else statement while the isNull() method is used by directly calling it from the Objects class.
In addition to this, you can use the “isEmpty()” and the “isBlank()” methods with the “==” operator to address the empty and blank strings. In this guide, we have discussed all these methods along with examples to check if a string is Null or not.