CodeGym /Java Blog /Strings in Java /Java: Check if String is Null, Empty or Blank
Author
Vasyl Malik
Senior Java Developer at CodeGym

Java: Check if String is Null, Empty or Blank

Published in the Strings in Java group
In Java, it’s very easy to confuse a null, empty or a blank string for beginners. However, let’s go by the definition of each to draw a line of difference.

What is a “null” String in Java?

“A null String in Java is literally equal to a reserved word “null”. It means the String that does not point to any physical address.”
In Java programming language, a “nullString is used to refer to nothing. It also indicates that the String variable is not actually tied to any memory location.

Example using a null check

Very often in programming, a String is assigned null to represent that it is completely free and will be used for a specific purpose in the program. If you perform any operation or call a method on a null String, it throws the java.lang.NullPointerException. Here is a basic example illustrating declaration of a null String. It further shows how to check if it is a valid null String.

public class Example {

	public static void main(String[] args) {

		// check if it is a null string
		String myName = null;
		String nullString = null;
		
		
		if (myName == null) {
			// print if the string is null
			System.out.println("The String = " + myName);
		}

		// another way to check if a string is null
		if (myName == nullString) {
			System.out.println("Both strings are null.");
		}
		
		myName = "Lubaina Khan";
		if (myName != null) {
			System.out.println("The String = " + myName);
		}
	}
}

Output

Both strings are null. The String = null The String = Lubaina Khan

What is an “empty” String in Java?

“An empty String in Java means a String with length equal to zero.”
If a String is empty that means the reference variable is referring to a memory location holding a String of length equal to zero. In Java, a built-in method is available to check if a String is empty before performing any operations. If you don’t want to use this available method, alternatively you can check if the length of the String is zero. It will do the job for you. For the sake of this example, we are using the built-in method to see if the string is empty. Feel free to use the “length” check for your practice. Also, you can use the example below to check if a string is null or empty.

Example using an empty check


public class Example1 {

	public static void main(String[] args) {

		// check if it is an "empty" string

		String myName = new String();

		System.out.println("The String = " + myName);

		// not sure if the string is either null or empty

		System.out.println("Is the String null? " + (myName == null));

		System.out.println("Is the String empty? " + myName.isEmpty());

		// will go in the 'if block' if any one of the checks are true
		if (myName != null || myName.isEmpty()) { 

			myName = "Lubaina Khan"; 
			System.out.println("The String = " + myName);
		}
	}
}

Output

The String = Is the String null? false Is the String empty? true The String = Lubaina Khan

What is a “blank” String in Java?

“A “blankString in Java is equal to a String with one or multiple spaces.”
As mentioned before, a “blankString is different from a scenario where a String is null or empty. There are cases when a String can hold a space, a lot of spaces, tabs or new line characters that are not mostly useful. Java provides a built-in method to check for all those whitespaces in a String. Let’s look at an example on how to use that.

Example using a blank check


public class Example2 {

	public static void main(String[] args) {

		// check if it is a "blank" string

		String myName = new String("   \t  \n    \t \t   ");

		System.out.println("The String = " + myName);

		System.out.println("Is the String null? " + (myName == null));

		System.out.println("Is the String empty? " + myName.isEmpty());
		
		System.out.println("Is the String blank? " + myName.isBlank());

		myName = myName.concat("Lubaina Khan");
		if (!myName.isEmpty()) {
			System.out.println("The String = " + myName);
		}
	}
}

Output

The String = Is the String null? false Is the String empty? false Is the String blank? true The String = Lubaina Khan

Conclusion

Here was a quick run down on how a null, empty and blank Strings can be spotted and checked in Java. By now you must be clear on how different they are from each other and when you have to check for them. We encourage you to learn by execution. As always, we would love to respond to any queries you might have. Happy learning!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION