Correct way to check for null or empty or string containing only spaces is like this:

if(str != null && !str.trim().isEmpty()) { /* do your stuffs here */ }
Answer from Pradeep Simha on Stack Overflow
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ java: check if string is null, empty or blank
Java: Check if String is Null, Empty or Blank
October 11, 2023 - The String = Is the String null? false Is the String empty? true The String = Lubaina Khan ยท โ€œA โ€œblankโ€ String in Java is equal to a String with one or multiple spaces.โ€ As mentioned before, a โ€œblankโ€ String is different from a scenario where a String is null or empty.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ difference between null and empty string in java
Difference Between null and Empty String in Java | Baeldung
April 19, 2024 - By default, Java initializes reference variables with null values and primitives with default values based on their type. As a result, we cannot assign null to primitives. If we assign null to a String object, itโ€™s initialized but not instantiated and hence holds no value or reference. An empty String is a valid String object having no characters, and as a result, all the String operations are available on this object.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javaee โ€บ 7 โ€บ tutorial โ€บ bean-validation002.htm
21.2 Validating Null and Empty Strings - Java Platform, Enterprise Edition: The Java EE Tutorial (Release 7)
if (testString==null) { doSomething(); } else { doAnotherThing(); } By default, the doAnotherThing method is called even when the user enters no data, because the testString element has been initialized with the value of an empty string. In order for the Bean Validation model to work as intended, you must set the context parameter javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL to true in the web deployment descriptor file, web.xml:
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ core java โ€บ replace null with empty string in java
Replace null with Empty String in Java - Java2Blog
November 28, 2023 - If a string is null, itโ€™s replaced with an empty string. Performance: Efficient for smaller lists, but performance may degrade with very large lists due to the iterative nature. For a more modern approach, Java 8โ€™s Stream API can be used to streamline this process.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ null array to empty list in java
Null Array to Empty List in Java | Baeldung
April 7, 2025 - Here, weโ€™ll return an empty array if the original array is null before converting the array to a list. Both approaches above safely return a List of Strings with a single expression. Before Java 8, we can also use a simple ternary operator to check for null and return an empty List when our array is null:
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ checking for empty or blank strings in java
Checking for Empty or Blank Strings in Java | Baeldung
January 8, 2024 - String#isEmpty was introduced with Java 6. For Java 5 and below, we can use String#length instead: boolean isEmptyString(String string) { return string == null || string.length() == 0; } In fact, String#isEmpty is just a shortcut to String#length. Both String#isEmpty and String#length can be used to check for empty strings.
Find elsewhere
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ difference between null and empty strings in java
Null and Empty String in Java | Delft Stack
October 12, 2023 - Null is like a vacuum with some property associated with it, due to which we can neither consider it to be empty nor full. In Java, a string refers to the sequence of characters.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ checking if an array is null or empty in java
Checking if an Array Is Null or Empty in Java | Baeldung
July 16, 2024 - Checking if an array is null or empty is a fundamental task in Java programming. In this article, weโ€™ve explored how to implement these checks for both object and primitive type arrays, creating a robust utility method that can be reused in our applications.
๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ core java
Converting a Null Array to an Empty List in Java - Java Code Geeks
February 3, 2025 - If the array is null, the orElseGet method provides an empty stream as a fallback. The stream is then collected into a list using Collectors.toList. When working with older versions of Java or if you prefer a more explicit approach, the ternary ...
Top answer
1 of 16
409

You may also understand the difference between null and an empty string this way:

Original image by R. Sato (@raysato)

2 of 16
251

"" is an actual string, albeit an empty one.

null, however, means that the String variable points to nothing.

a==b returns false because "" and null do not occupy the same space in memory--in other words, their variables don't point to the same objects.

a.equals(b) returns false because "" does not equal null, obviously.

The difference is though that since "" is an actual string, you can still invoke methods or functions on it like

a.length()

a.substring(0, 1)

and so on.

If the String equals null, like b, Java would throw a NullPointerException if you tried invoking, say:

b.length()


If the difference you are wondering about is == versus equals, it's this:

== compares references, like if I went

String a = new String("");
String b = new String("");
System.out.println(a==b);

That would output false because I allocated two different objects, and a and b point to different objects.

However, a.equals(b) in this case would return true, because equals for Strings will return true if and only if the argument String is not null and represents the same sequence of characters.

Be warned, though, that Java does have a special case for Strings.

String a = "abc";
String b = "abc";
System.out.println(a==b);

You would think that the output would be false, since it should allocate two different Strings. Actually, Java will intern literal Strings (ones that are initialized like a and b in our example). So be careful, because that can give some false positives on how == works.

๐ŸŒ
Stack Abuse
stackabuse.com โ€บ java-check-if-string-is-null-empty-or-blank
Java: Check if String is Null, Empty or Blank
February 28, 2023 - 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: ... 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.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ check-if-a-string-is-empty-or-null
Java Program to Check if a String is Empty or Null | Vultr Docs
December 17, 2024 - The Java language provides various ways to handle this, ensuring robustness and preventing runtime errors like NullPointerException from derailing your application. In this article, you will learn how to effectively check if a string is empty or null using several methods provided by Java.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ string-empty-null
Java Program to Check if a String is Empty or Null
class Main { public static void main(String[] args) { // create a string with white spaces String str = " "; // check if str1 is null or empty System.out.println("str is " + isNullEmpty(str)); } // method check if string is null or empty public static String isNullEmpty(String str) { // check if string is null if (str == null) { return "NULL"; } // check if string is empty else if (str.trim().isEmpty()){ return "EMPTY"; } else { return "neither NULL nor EMPTY"; } } }
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 10 โ€บ check-if-string-is-null-empty-or-blank-in-java
Check if String is Null, Empty or Blank in Java
January 7, 2023 - String myString = ""; //empty string if(myString!=null && myString.isEmpty()){ System.out.println("This is an empty 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 ...