Java 101: Check if a String Is Empty or Blank

In Java, an empty String and a blank String are two different concepts. It’s always confusing and tricky for Java beginners to check if a String is both, empty or blank. An empty String is a String object initialized without any character, whereas a blank string is a String with a whitespace character. Always remember that a String with just whitespace may or may not be considered empty, depending on the program. Depending upon your situation, you can include the logic to check for that as well. A String with just whitespace is also referred to as a blank String in Java. In this tutorial, I will teach you a couple of right ways to check if a String is empty or blank in Java.

Check if a String Is Null or Empty in Java

To make sure we’re on the same page with our definitions, we consider a String to be empty if it’s either null or a string without any length. If a string consists of whitespace only, then we call it blank.