Java checking if string is null

Java: Check if String is Null, Empty or Blank

In Java, there is a distinct difference between null , empty, and blank Strings.

  • An empty string is a String object with an assigned value, but its length is equal to zero.
  • A null string has no value at all.
  • A blank String contains only whitespaces, are is neither empty nor null , since it does have an assigned value, and isn’t of 0 length.
String nullString = null; String emptyString = ""; String blankString = " "; 

In this tutorial, we’ll look at how to check if a String is Null, Empty or Blank in Java.

Using the Length of the String

As mentioned before, a string is empty if its length is equal to zero. We will be using the length() method, which returns the total number of characters in our string.

String blankString = " "; if (blankString == null || blankString.length() == 0) System.out.println("This string is null or empty"); else System.out.println("This string is neither null nor empty"); 

The code above will produce the following output:

This string is null or empty 

The String is blank, so it’s obviously neither null nor empty. Now, based just on the length, we can’t really differentiate between Strings that only contain whitespaces or any other character, since a whitespace is a Character .

Читайте также:  Programming projects with python

Note: It’s important to do the null -check first, since the short-circuit OR operator || will break immediately on the first true condition. If the string, in fact, is null , all other conditions before it will throw a NullPointerException .

Using the isEmpty() Method

The isEmpty() method returns true or false depending on whether or not our string contains any text. It’s easily chainable with a string == null check, and can even differentiate between blank and empty strings:

String string = "Hello there"; if (string == null || string.isEmpty() || string.trim().isEmpty()) System.out.println("String is null, empty or blank."); else System.out.println("String is neither null, empty nor blank"); 

The trim() method removes all whitespaces to the left and right of a String, and returns the new sequence. If the String is blank, after removing all whitespaces, it’ll be empty, so isEmpty() will return true .

Running this piece of code will give us the following output:

String is neither null, empty nor blank 

Using the equals() Method

The equals() method compares the two given strings based on their content and returns true if they’re equal or false if they are not:

String string = "Hello there"; if (string == null || string.equals("") || string.trim().equals("")) System.out.println("String is null, empty or blank"); else System.out.println("String is neither null, empty nor blank"); 

In much the same fashion as the before, if the trimmed string is «» , it was either empty from the get-go, or was a blank string with 0..n whitespaces:

String is neither null, empty nor blank 

Using the StringUtils Class

The Apache Commons is a popular Java library that provides further functionality. StringUtils is one of the classes that Apache Commons offers. This class contains methods used to work with Strings , similar to the java.lang.String .

If you’re unfamiliar with Apache Commons’ helper classes, we strongly suggest reading our Guide to the StringUtils class.

Since we’ll be using Apache Commons for this approach, let’s add it as a dependency:

dependency> groupId>org.apache.commons groupId> artifactId>commons-lang3 artifactId> version>3.11 version> dependency> 
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11' 

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

One of the key differences between StingUtils and String methods is that all methods from the StringUtils class are null-safe. It additionally provides a few methods that we can leverage for this, including StringUtils.isEmpty() and StringUtils.isBlank() :

String nullString = null; if (nullString == null) < System.out.println("String is null"); > else if (StringUtils.isEmpty(nullString)) < System.out.println("String is empty"); > else if (StringUtils.isBlank(nullString)) < System.out.println("String is blank"); > 

In addition to these, their inverse methods also exist: StringUtils.isNotEmpty() and StringUtils.isNotBlank() , though, you can achieve the same functionality by using the NOT ( ! ) operator:

if (StringUtils.isNotEmpty("")) System.out.println("String is not empty"); // Equivalent to if (!StringUtils.isEmpty("")) System.out.println("String is not empty"); 

Conclusion

A string is an object that represents a sequence of characters. Java provides many different methods for string manipulation. In this article, we have used some of these methods such as isEmpty() , equals() , StringUtils.isEmpty() and length() to check if the String is null , empty or blank.

Источник

Check if String is Null, Empty or Blank in Java

In this guide, we will learn how to check if a string is null, empty or blank. First let’s see what is the difference between null, empty or blank string in java.

What is Null String?

A string with no assigned value. For example:

The length of null string is zero and the value is equal to null. To check for null string simply compare the String instance with null, if it is equal to null that means it is a null string. For example:

String myString = null; //null string if(myString==null)

What is an Empty String?

An empty string has a value assigned to it but the length is zero. For example:

String str = ""; //there is no space between quotes

The length of empty string is zero and the value is not equal to null. You can check for empty String like this. The isEmpty() method returns true if the string is empty.

String myString = «»; //empty string if(myString!=null && myString.isEmpty())

What is Blank String?

A blank string contains only whitespaces.

String str = " "; //there is a whitespace between quotes

The length of a blank string is not zero, the isEmpty() method doesn’t return true for blank String. However there is a way to check blank string combining the isEmpty() method with trim() method.

Since we know that the blank string contain whitespaces, we can trim the whitespaces using trim() method and then call isEmpty() method. This method is shown in the following example.

Example: How to check null, blank and empty String

  • Simply compare the string with null to check for null string.
  • Use isEmpty() method of string class to check for empty string. The isEmpty() method returns true if the string does not contain any value.
  • Use trim() and isEmpty() method together to check for blank string. The trim() method remove all whitespaces and then isEmpty() checks if the string contains any value after removing whitespaces.
public class JavaExample < public static void main(String[] args) < String str1 = null; //null string String str2 = ""; //empty string String str3 = " "; //blank string if(str1==null)< System.out.println("str1 is null string"); >else if(str1.isEmpty())< System.out.println("str1 is empty string"); >else if(str1.trim().isEmpty()) < System.out.println("str1 is blank string"); >if(str2==null)< System.out.println("str2 is null string"); >else if(str2.isEmpty())< System.out.println("str2 is empty string"); >else if(str2.trim().isEmpty()) < System.out.println("str2 is blank string"); >if(str3==null)< System.out.println("str3 is null string"); >else if(str3.isEmpty())< System.out.println("str3 is empty string"); >else if(str3.trim().isEmpty()) < System.out.println("str3 is blank string"); >> >

Java String null, empty or blank check example

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

Check if a String Is Empty or Null in Java

Check if a String Is Empty or Null in Java

  1. Use str == null to Check if a String Is null in Java
  2. Use str.isEmpty() to Check if a String Is Empty in Java

This tutorial discusses methods to check if a string is empty or null in Java.

Use str == null to Check if a String Is null in Java

The simplest way to check if a given string is null in Java is to compare it with null using str == null . The below example illustrates this:

public class MyClass   public static void main(String args[])   String str1 = null;  String str2 = "Some text";  if (str1 == null)  System.out.println("str1 is a null string");  else  System.out.println("str1 is not a null string");   if (str2 == null)  System.out.println("str2 is a null string");  else  System.out.println("str2 is not a null string");  > > 
str1 is a null string str2 is not a null string 

Use str.isEmpty() to Check if a String Is Empty in Java

The simplest way to check if a given string is empty in Java is to use the built-in method of String class — isEmpty() . The below example illustrates this:

public class MyClass   public static void main(String args[])   String str1 = "";  String str2 = "Some text";  if (str1.isEmpty())  System.out.println("str1 is an empty string");  else  System.out.println("str1 is not an empty string");   if (str2.isEmpty())  System.out.println("str2 is an empty string");  else  System.out.println("str2 is not an empty string");  > > 
str1 is an empty string str2 is not an empty string 

If we are interested in checking for both of the conditions at the same time, we can do so by using logical OR operator — || . The below example illustrates this:

public class MyClass   public static void main(String args[])   String str1 = "";  if (str1.isEmpty() || str1 == null)  System.out.println("This is an empty or null string");  else  System.out.println("This is neither empty nor null string");  > > 
This is an empty or null string 

Related Article — Java String

Источник

Оцените статью