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
🌐
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 - 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.
🌐
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.
🌐
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".
🌐
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.
🌐
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.
🌐
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.
🌐
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
String myString = null; //null string if(myString==null){ System.out.println("This is a null 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
Find elsewhere
🌐
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"; } } }
🌐
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 - 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 ...
🌐
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
February 29, 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.
🌐
TestMu AI Community
community.testmuai.com › ask a question
Best Way to Check if a String is Null or Empty in Java - TestMu AI 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.
🌐
Java67
java67.com › 2014 › 09 › right-way-to-check-if-string-is-empty.html
Right way to check if String is empty in Java with Example | Java67
An empty Java String is considered as the not null String that contains zero characters, meaning its length is 0. However, a Java String that might only contain the white-space character is not considered empty, it is considered to contain one character and its length is equal to 1. One of the most popular ways of checking whether String is empty or not is the String class' isEmpty() method...
🌐
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
🌐
TutorialsPoint
tutorialspoint.com › checking-for-null-or-empty-in-java
Checking for Null or Empty in Java.
We can check whether a particular String is empty or not, using the isBlank() method of the StringUtils class. This method accepts an integer as a parameter and returns true if the given string is empty, or false if it is not. The following is an example of checking if a string is null or 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.
🌐
Medium
medium.com › @lp.lok.payu › isempty-vs-empty-vs-isblank-vs-isnull-12aea580fe4b
isEmpty() vs empty() vs isBlank() vs isNull() | by leela prasad | Medium
February 16, 2025 - Returns true if the string is null, empty, or only whitespace. The isNull() method is not a built-in Java method but is commonly implemented in utility classes to check for null references.
🌐
Coderanch
coderanch.com › t › 693624 › java › check-null-empty-strings-time
Do we have any way to check null value and empty strings all the time ? (Features new in Java 8 forum at Coderanch)
Here in above code, i need to check for Empty String all time manually and also i need to write code for null value check before empty strings checking. So, i am writing same code again and again for that purpose specially. Do we have any way, please suggest me ? ... Don't you have a method that tests for nullity? Remember the zero‑length String is different from null.