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
🌐
Reddit
reddit.com › r/learnjava › shorter (efficient) way to check if a string is null or empty?
Shorter (efficient) way to check if a string is null or empty? : r/learnjava
May 16, 2020 - The if statement says "if it's null OR it's empty" So, if the string is null the "isEmpty" part will NOT be executed.
🌐
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 - Like Guava, it won’t check if a string only contains whitespace but checks whether a given string is null or empty. There are several ways to check whether a string is empty or not. Often, we also want to check if a string is blank, meaning that it consists of only whitespace characters. The most convenient way is to use Apache Commons Lang, which provides helpers such as StringUtils.isBlank. If we want to stick to plain Java, we can use a combination of String#trim with either String#isEmpty or String#length.
🌐
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 - Here, StringUtils.isBlank() checks if str is null, empty, or made solely of whitespace (spaces, tabs, new line characters). Very useful when the mere presence of whitespace should also classify the string as "empty". Recognize the enhancements in Java 11 including new String methods.
🌐
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.
🌐
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 - “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 “null” String is used to refer to nothing.
🌐
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)
Oracle | Hardware and Software, Engineered to Work Together ... The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all.
Find elsewhere
🌐
Medium
medium.com › @ecetasci.iu › checking-for-null-or-empty-strings-in-java-19518fa1e553
Checking for Null or Empty Strings in Java | by Ece Tasci | Medium
February 25, 2025 - To prevent this, it is crucial to check for null before performing operations on a variable. An empty String ("") has zero characters but still exists in memory, specifically in the heap where Java stores objects.
🌐
TutorialsPoint
tutorialspoint.com › checking-for-null-or-empty-in-java
Checking for Null or Empty in Java.
August 4, 2025 - The length() method of the String class returns the number of characters in a string. If the length is 0, then the string is empty. The following is an example of checking if a string is null or empty using the length() method:
🌐
Quora
quora.com › Is-there-a-better-way-to-do-an-empty-check-in-Java
Is there a better way to do an empty check in Java? - Quora
Answer (1 of 2): You’ve got the Apache StringUtils stuff which has methods like isNotBlank and methods to use a default String. Great for null/empty/space checks on String More generally, there are many ways of conveying empty in Java. None of them are good.
🌐
Java Guides
javaguides.net › 2019 › 04 › check-if-string-is-empty-or-null-in-java-utility-methods.html
How to Check if a String is Null or Empty in Java
August 2, 2023 - Empty String: An empty string is a string object that exists in memory, but it contains no characters. It has a length of 0. The equals() method can be used to check if a string is null-safe and prevents a NullPointerException.
🌐
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 - Apache Commons Lang 3 is a widely used library. Its ArrayUtils class provides a rich set of utilities, making array manipulation easier. The class offers an isEmpty() method to check whether an array is null or empty.
🌐
Codemia
codemia.io › knowledge-hub › path › checking_if_a_string_is_empty_or_null_in_java
Checking if a string is empty or null in Java
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Programiz
programiz.com › java-programming › examples › string-empty-null
Java Program to Check if a String is Empty or Null
Become a certified Java programmer. Try Programiz PRO! ... class Main { public static void main(String[] args) { // create null, empty, and regular strings String str1 = null; String str2 = ""; String str3 = " "; // check if str1 is null or empty System.out.println("str1 is " + isNullEmpty(str1)); // check if str2 is null or empty System.out.println("str2 is " + isNullEmpty(str2)); // check if str3 is null or empty System.out.println("str3 is " + isNullEmpty(str3)); } // 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.isEmpty()){ return "EMPTY"; } else { return "neither NULL nor EMPTY"; } } }
🌐
Blogger
javarevisited.blogspot.com › 2016 › 01 › how-to-check-if-string-is-not-null-and-empty-in-java-example.html
How to check if String is not null and empty in Java? Example
if(stirng != null && string.trim().length() > 0){ System.out.println("String is not null and not empty"); } You can use this solution if your program doesn't consider a String with only white-space as a non-empty. If you load data from the database or stored data into the database it's better to trim() them before using them to avoid pesky issues due to whitespaces. That's all about how to check if String is not null and not empty in Java.
🌐
LambdaTest Community
community.lambdatest.com › general discussions
Best Way to Check if a String is Null or Empty in Java - LambdaTest Community
March 19, 2025 - What is the best way to check if a String is null or empty in Java? I’m currently parsing HTML data, and sometimes the String can be null or empty when the expected word doesn’t match. To handle this, I wrote the following check: if(string.equals(null) || string.equals("")){ Log.d("iftrue", "seem to be true"); }else{ Log.d("iffalse", "seem to be false"); } However, when I remove string.equals(""), the condition doesn’t work correctly.
🌐
Oracle
docs.oracle.com › cd › E19798-01 › 821-1841 › gkcrg › index.html
Validating Null and Empty Strings (The Java EE 6 Tutorial)
The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all.
🌐
Java2Blog
java2blog.com › home › core java › replace null with empty string in java
Replace null with Empty String in Java - Java2Blog
November 28, 2023 - Use Cases: Ideal for straightforward null checks where performance is a concern. In Java 9 and later, you can use Objects.requireNonNullElse from java.util.Objects. ... Objects.requireNonNullElse(str, ""): Returns str if it’s non-null; otherwise, returns an empty string. It’s a clean and readable way to handle null values. Performance: Comparable to the ternary operator, with slightly more overhead due to the method call. Use Cases: Recommended for applications using Java 9 or newer, especially where readability and standard library solutions are preferred.
🌐
W3Schools
w3schools.com › java › ref_string_isempty.asp
Java String isEmpty() Method
String myStr1 = "Hello"; String myStr2 = ""; System.out.println(myStr1.isEmpty()); System.out.println(myStr2.isEmpty()); ... The isEmpty() method checks whether a string is empty or not.
🌐
Liberian Geek
liberiangeek.net › home › how-to/tips › how to check if an object is null in java?
How to Check if an Object is Null in Java? | Liberian Geek
February 22, 2024 - However, as a recommendation according ... be used. In Java, you can check if an object is null by using the '==' operator to compare the object reference with null....