The main benefit of "".equals(s) is you don't need the null check (equals will check its argument and return false if it's null), which you seem to not care about. If you're not worried about s being null (or are otherwise checking for it), I would definitely use s.isEmpty(); it shows exactly what you're checking, you care whether or not s is empty, not whether it equals the empty string

Answer from Michael Mrozek on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_isempty.asp
Java String isEmpty() Method
The isEmpty() method checks whether a string is empty or not.
Discussions

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
java - Should I use string.isEmpty() or "".equals(string)? - Stack Overflow
I'm usually testing this alongside a string == null, so I'm not really concerned about a null-safe test. Which should I use? String s = /* whatever */; ... if (s == null || "".equals(s)) ... More on stackoverflow.com
🌐 stackoverflow.com
java - Best way to verify string is empty or null - Stack Overflow
You can also use Apache String utils isEmpty() this does the null check as well as space checks. More on stackoverflow.com
🌐 stackoverflow.com
!isEmpty vs != “”
IIRC isEmpty checks a 1 bit flag on the collection, whereas at the very minimum checking against “” instantiates a string to compare against on the heap, which is comparatively astronomically expensive More on reddit.com
🌐 r/swift
26
10
January 5, 2024
🌐
Microsoft Learn
learn.microsoft.com › en-us › office › vba › language › reference › user-interface-help › isempty-function
IsEmpty function (Visual Basic for Applications) | Microsoft Learn
September 13, 2021 - IsEmpty returns True if the variable is uninitialized, or is explicitly set to Empty; otherwise, it returns False. False is always returned if expression contains more than one variable.
🌐
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.
Find elsewhere
🌐
MuleSoft
docs.mulesoft.com › dataweave › latest › dw-core-functions-isempty
isEmpty | MuleSoft Documentation
%dw 2.0 output application/json --- [ isEmpty({}), isEmpty({name: "DataWeave"}) ]
🌐
Lucee Documentation
docs.lucee.org › reference › objects › struct › struct.isempty()
IsEmpty() :: Lucee Documentation
writeOutput('Strings'); writeDump(IsEmpty( '' )); // true; writeDump(IsEmpty( ' ' )); // false; writeDump(IsEmpty( '0' )); // false; writeOutput('arrays'); writeDump(IsEmpty( [] )); // true; writeDump(IsEmpty( [ 1, 2, 3 ] )); // false; writeOutput('structs'); writeDump(IsEmpty( {} )); // true; writeDump(IsEmpty( { key="value" } )); // false; writeOutput('queries'); writeDump(IsEmpty( QueryNew( 'column' ) )); // true; writeDump(IsEmpty( QueryNew( 'column', 'varchar', [ [ 'value' ] ] ) )); // false; writeOutput('numerics always non-empty'); writeDump(IsEmpty( 0 )); // false; writeDump(IsEmpty( 1 )); // false; writeOutput('booleans always non-empty'); writeDump(IsEmpty( false )); // false; writeDump(IsEmpty( true )); // false;
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - public boolean isEmpty() Returns true if, and only if, length() is 0. Returns: true if length() is 0, otherwise false · Since: 1.6 · public char charAt(int index) Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is ...
🌐
HCL Software
help.hcl-software.com › dom_designer › 9.0.1 › reference › r_wpdr_standard_string_isempty_r.html
isEmpty (JavaScript)
isEmpty() : boolean · An empty string has a length of 0. For an empty string, s=="" is true, but s==null is false. (1) This example returns the content of cities. var cities = "Paris"; // Should not be empty if (cities.isEmpty()) { return "Empty"; } else { return "'" + cities + "'"; } (2) This example returns Empty.
🌐
Codecademy
codecademy.com › docs › java › queue › .isempty()
Java | Queue | .isEmpty() | Codecademy
August 7, 2025 - The .isEmpty() method is an inbuilt method of the Queue interface in Java that returns true if the queue contains no elements, and false otherwise. It is inherited from the Collection interface and provides a convenient way to check if a queue ...
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.collections › -collection › is-empty.html
isEmpty | Core API – Kotlin Programming Language
import kotlin.math.* import ... } actual abstract fun isEmpty(): Boolean(source) Returns true if the collection is empty (contains no elements), false otherwise....
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-isempty-method-example
Java String isEmpty() Method with Example - GeeksforGeeks
November 20, 2024 - In Java, the String isEmpty() method checks if a string is empty (length is zero). This method returns true if the string is empty and false otherwise.
🌐
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 ...
🌐
Baeldung
baeldung.com › home › java › java string › java string.isempty()
Java.String.isEmpty() | Baeldung
April 11, 2025 - The method isEmpty() is a convenience method that checks if the size of a String is equal to zero.
🌐
Medium
medium.com › @AlexanderObregon › java-string-isempty-method-explained-a731faf082aa
Java String isEmpty() Method Guide | Medium
June 23, 2024 - The isEmpty() method is defined in the String class and has a very straightforward functionality. It returns a boolean value: true if the string has a length of zero, and false otherwise.
🌐
Microsoft Support
support.microsoft.com › en-us › office › isempty-function-a86d5871-f6bd-455c-9256-a69a42e55e50
IsEmpty Function - Microsoft Support
IsEmpty returns True if the variable is uninitialized, or is explicitly set to Empty; otherwise, it returns False. False is always returned if expression contains more than one variable.