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 - String#isEmpty was introduced with ... String#isEmpty is just a shortcut to String#length. Both String#isEmpty and String#length can be used to check for empty strings. If we also want to detect blank strings, we can achieve this ...
๐ŸŒ
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 - When working with Strings in Java, it is common to encounter situations where a String might be either null or empty. This can lead to unexpected errors, particularly when working with user input, API responses, or database records. To avoid these issues, performing proper checks before using a String in operations like concatenation, comparison, or storage is essential. One of the most common ways to ensure a String is valid before using it is by checking if(name != null && !name.isEmpty()).
๐ŸŒ
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.
๐ŸŒ
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 = null The String = Lubaina Khan ยท โ€œ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.
๐ŸŒ
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
To check for null string simply compare the String instance with null, if it is equal to null that means it is a null string. For example: 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 ...
๐ŸŒ
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 "", ...
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"; } } }
๐ŸŒ
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 ...
๐ŸŒ
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
March 1, 2020 - [The Java String class has an isEmpty method](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#isEmpty() ... That's correct, but his left hand condition will check for that, if its true the right hand condition will not be evaluated ... This is TERRIBLE advice. The point of evaluating a variable is so that the program doesn't crash. Yet, your advice is to let the application crash. That is in no way better than checking that the variable is null or empty.
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
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 you are a complete beginner then I suggest you first go through a comprehensive course like The Complete Java Masterclass on Udemy to learn more about core Java basics as well as such gems from Java API. Here are my three solutions to this common problem. Each solution has its pros and cons and a special use case like the first solution can only be used from JDK 7 onward, the second is the fastest way to check if String is empty and the third solution should be used if your String contains whitespaces. This is the most readable way to check for both whether String is null or not and whether String is empty or not.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 203109 โ€บ java โ€บ Efficient-Check-Empty-String
Efficient Way to Check Empty String (Performance forum at Coderanch)
We use (string != null || string.trim().length() == 0) to check if string is empty. Calling a method which actually iterate through string and checks for non-whitespace character .
๐ŸŒ
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.
๐ŸŒ
Quora
quora.com โ€บ How-do-perform-a-Null-check-for-string-in-Java
How do perform a Null check for string in Java? - Quora
Answer (1 of 7): 4 Ways to check if String is null or empty in Java:- Here are my four solutions for this common problem. Each solution has their pros and cons and a special use case e.g. first solution can only be used from JDK7 on wards, second solution is the fastest way to check string is em...
๐ŸŒ
Apps Developer Blog
appsdeveloperblog.com โ€บ home โ€บ java โ€บ check if a string is null, empty or blank in java
Check if a String is Null, Empty or Blank in Java
June 4, 2023 - Learn how to effectively check if a Java string is null, empty, and whitespace. Enhance code reliability and prevent errors. Master string validation in Java now!