Copyif (myString != null && !myString.isEmpty()) {
  // doSomething
}

As further comment, you should be aware of this term in the equals contract:

From Object.equals(Object):

For any non-null reference value x, x.equals(null) should return false.

The way to compare with null is to use x == null and x != null.

Moreover, x.field and x.method() throws NullPointerException if x == null.

Answer from polygenelubricants on Stack Overflow
๐ŸŒ
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
๐ŸŒ
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.
Discussions

java - How to check if my string is equal to null? - Stack Overflow
Apache commons StringUtils.isNotEmpty is the best way to go. ... Save this answer. Show activity on this post. If myString is in fact null, then any call to the reference will fail with a Null Pointer Exception (NPE). Since java 6, use #isEmpty instead of length check (in any case NEVER create a new empty ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Question about .isEmpty() and .contains("")
isEmpty() checks for string length and returns true if it's zero, while contains("") will return true unless the string isn't initialized/null. For checking empty strings or strings containing only whitespace you could also use the method isBlank(). Hope this helps! More on reddit.com
๐ŸŒ r/learnjava
7
5
July 25, 2023
Shorter (efficient) way to check if a string is null or empty?
[The Java String class has an isEmpty method]( https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#isEmpty() More on reddit.com
๐ŸŒ r/learnjava
12
2
March 2, 2020
In Java, should I use (String ) isEmpty() or isBlank() when checking an XML object for an empty value?
What behavior do you want if the xml parser emits " " as the config value? More on reddit.com
๐ŸŒ r/AskProgramming
4
1
September 26, 2022
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2017 โ€บ 10 โ€บ java-string-isempty-method-with-example
Java String isEmpty() method
June 9, 2024 - Java String isEmpty() method checks whether a String is empty or not. This method returns true if the given string is empty, else it returns false.
๐ŸŒ
w3resource
w3resource.com โ€บ java-exercises โ€บ lambda โ€บ java-lambda-exercise-2.php
Java Program: Lambda Expression to Check if String is Empty
The lambda expression uses the isEmpty() method of the String class to determine if the string is empty. To check if the strings are empty, we create two test cases, str1 and str2, and use the test() method of the Predicate interface.
๐ŸŒ
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 ... One of the most popular ways of checking whether String is empty or not is the String class' isEmpty() method, this looks perfect right, it's readable and returns boolean if String is empty ...
๐ŸŒ
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 - In Java, a built-in method is available to check if a String is empty before performing any operations. If you donโ€™t want to use this available method, alternatively you can check if the length of the String is zero. It will do the job for you. For the sake of this example, we are using the built-in method to see if the string is empty.
Find elsewhere
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ String.html
String (Java Platform SE 8 )
October 20, 2025 - Returns a string whose value is this string, with any leading and trailing whitespace removed. If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.
๐ŸŒ
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ question about .isempty() and .contains("")
r/learnjava on Reddit: Question about .isEmpty() and .contains("")
July 25, 2023 -

SORRY

I JUST REALISED I MADE A MISTAKE I WANTED TO ASK ABOUT EQUALS("") BUT AT THE SAME TIME I WAS FIGHTING WITH ANOTHER EXCERCISE IN WHICH I HAD TO USE CONTAINS() METHOD AND THIS MESSED UP MY THOUGHTS SORRY FOR WASTING YOUR TIME :/

but still i was able to get some usefull info about contains method so it wasn't as much wasted effort but still sorry

Hi i'm doing(or atleast trying to do) mooc.fi java part 1 course and there in some exercises is required to check for empty input.

So there is my question what is difference between .isEmpty() and .Equals("") because i like to use the first one but in suggested solutions always is used ".Equals("") and i'm wondering is there any rule for using .isEmpty(), maybe it's the matter of optimalization etc. or maybe both are as good

I know it can be stupid question but it's better to ask that and get rid of bad habits in writing code as soon as possible :D

In advance thanks for answers

P.S i hope everything is easy to understand unfortunately my english is still far from good :D

Top answer
1 of 5
2
isEmpty() checks for string length and returns true if it's zero, while contains("") will return true unless the string isn't initialized/null. For checking empty strings or strings containing only whitespace you could also use the method isBlank(). Hope this helps!
2 of 5
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ string-empty-null
Java Program to Check if a String is Empty or Null
This is because white spaces are treated as characters in Java and the string with white spaces is a regular string. Now, if we want the program to consider strings with white spaces as empty strings, we can use the trim() method. The method removes all the white spaces present in a string. ...
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ java-string-isempty-method-explained-a731faf082aa
Java String isEmpty() Method Guide | Medium
June 23, 2024 - Learn about Java's isEmpty() method to efficiently check if a string is empty. Perfect for beginners and useful for technical interviews.
๐ŸŒ
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...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java โ€“ check if a string is empty or null
Java - Check if a String is empty or null - Mkyong.com
April 29, 2019 - In Java, we can use (str != null && !str.isEmpty()) to make sure the String is not empty or null. ... package com.mkyong; public class StringNotEmpty { public static void main(String[] args) { System.out.println(notEmpty("")); // false ...
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ lang โ€บ String โ€บ isEmpty
Java String isEmpty() - Check If Empty | Vultr Docs
December 17, 2024 - The isEmpty() method in Java is a straightforward and efficient way for checking whether a given string has a length of zero, which means it is an empty string. This method is part of the String class in Java's standard library, making it readily ...
๐ŸŒ
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
Since we are first doing a null check and then an empty check using the && operator, which is a short circuit AND operator. This operator will not check for emptiness if String is null hence no NPE. This is also a good trick to avoid NPE in Java.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java-program-to-check-if-a-string-is-empty-or-null
Java program to check if a string is empty or null
Print that it's an empty string if true. Otherwise, print that the string is neither null nor empty. ... In the main, define a string variable input_string and set it to null. Print the value of input_string. Call the isNullEmpty method with input_string as an argument to check its state.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ program-to-check-if-the-string-is-empty-in-java
Program to check if the String is Empty in Java - GeeksforGeeks
July 12, 2025 - We can simply check if the string is empty or not using the isEmpty() method of String class Syntax: ... Print true if the above condition is true. Else print false. Below is the implementation of the above approach: ... // Java Program to check ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ checking if a stringbuilder is null or empty
Checking if a StringBuilder Is Null or Empty | Baeldung
August 25, 2024 - Next, letโ€™s first look at the null checking part. Checking whether a StringBuilder is null isnโ€™t a challenge to us. In Java, we can verify any object is null using theObject == null:
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ check-if-string-is-empty-or-null-javascript
How to Check if a String is Empty or Null in JavaScript โ€“ JS Tutorial
November 7, 2024 - In this tutorial, we'll explore the different ways of checking whether a string is empty or null in JavaScript and some best practices to follow when doing so. An empty string is a string that has no characters, while a null string is a string that has no value assigned to it. It's important to differentiate between the two, as they are not the same. For example, you have a form where a user can input their name. If ...