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
๐ŸŒ
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.
๐ŸŒ
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 - 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.
๐ŸŒ
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/askprogramming โ€บ in java, should i use (string ) isempty() or isblank() when checking an xml object for an empty value?
r/AskProgramming on Reddit: In Java, should I use (String ) isEmpty() or isBlank() when checking an XML object for an empty value?
September 26, 2022 -

I have some config file and it is deployed in many regions. Some regions have certain requirements for example lets say one region needed a username and password (basic auth). The current setup is that each deployment has it's own config file and only the instances where it is needed are these values included in the respective config.xml file.

So I have in my Java code some logic that says

'if this element is in the XML, set the corresponding variable with its value, if not do nothing'

So when I'm trying to see if that value is present or if the tag is empty/blank, which String utility should I use to check?

example code (default option left open as empty string):

if (my_config.getString(USER_ID_ELEMENT, "") != null
    & my_config.getString(PASSWORD_ELEMENT, "") != null) {
		my_userId = m_config.getString(USER_ID_ELEMENT, "");
		my_password = m_config.getString(PASSWORD_ELEMENT, "");

Here I'm checking if it is not null, but I don't think it's a very good test since I don't know if an empty XML tag is blank or is empty (i think null isn't even an option)?

๐ŸŒ
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 - Note: It's important to do the ... all other conditions before it will throw a NullPointerException. The isEmpty() method returns true or false depending on whether or not our string contains any text....
Find elsewhere
๐ŸŒ
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 - The isEmpty operator checks if a string contains no characters and is only whitespace. The isBlank operator checks if a string contains no characters, is only whitespace, and is null.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-string-isempty
Java String isEmpty()
Java String isEmpty() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string isempty in java etc.
๐ŸŒ
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 - " + myName.isEmpty()); // will ... with one or multiple spaces.โ€ As mentioned before, a โ€œblankโ€ String is different from a scenario where a String is null or empty....
๐ŸŒ
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 ... to 1. 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 otherwise returns false, but the problem ...
๐ŸŒ
BootcampToProd
bootcamptoprod.com โ€บ java-string-isempty-vs-isblank
Java String isEmpty vs isBlank: A Detailed Comparison - BootcampToProd
December 16, 2023 - An empty string is a string that has no characters, while a blank string is a string that has only whitespace characters. The isEmpty method was introduced in Java 6, and it returns true if and only if the length of the string is zero.
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ java 11 โ€บ java string isblank() โ€“ check blank or empty string
Java String isBlank() - Check Blank or Empty String
December 12, 2022 - Both methods are used to check for blank or empty strings in java. The difference between both methods is that: isEmpty() method returns true if, and only if, the string length is 0.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ difference between string isempty() and isblank()
Difference Between String isEmpty() and isBlank() | Baeldung
December 2, 2025 - In contrast, isEmpty() returns false for all of them. Finally, for any other character different from the ones shown in the table, both methods return false. Before Java 11, developers typically used the combination of String.trim() and ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-stringutilsisempty-in-java
What is StringUtils.isEmpty in Java?
The output of StringUtils.isEmpty() for the string - '543234' is false The output of StringUtils.isEmpty() for the string - '' is true The output of StringUtils.isEmpty() for the string - 'null' is true
๐ŸŒ
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)
An empty string is represented as "". It is a character sequence of zero characters. A null string is represented by null. It can be described as the absence of a string instance. Managed bean elements represented as a JavaServer Faces text component such as inputText are initialized with the ...