This sounds like a bad design to me. Null is null, empty is empty, if it's a string it's a string, and so on. Don't try to jam everything up in one method. It's bad for maintainability and readability.

if (str == null || str.isEmpty())
    ...

and

if (coll == null || coll.isEmpty())

are both perfectly fine.

Personally however, I try to never ever equate null with an empty string or empty collection. I think it's a bad practice. A null collection is no collection at all, an empty collection is in fact still a collection. You can avoid many if (coll == null) checks by keeping a collection non-null. If you're worried about memory consumption, use use Collections.emptySet et al.


That being said, if you still want to go in this direction, I'd suggest you use plain method overloading and create one isEmpty(Collection<?> coll) and one isEmpty(String str) to avoid instanceof and casting.


Regarding your edit:

Don't do for instance

if (value == null || value.isEmpty()) {
    return true;
}
return false;

just do

return value == null || value.isEmpty();
Answer from aioobe on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_isempty.asp
Java String isEmpty() Method
This method returns true if the string is empty (length() is 0), and false if not. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an ...
🌐
Codecademy
codecademy.com › docs › java › strings › .isempty()
Java | Strings | .isEmpty() | Codecademy
March 4, 2023 - The .isEmpty() method returns true if a string has no content. It returns false if the string has content. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
🌐
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 › library › string › isempty
Java String isEmpty()
Become a certified Java programmer. Try Programiz PRO! ... Here, string is an object of the String class. The isEmpty() method does not take any parameters.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java string isempty method
Java String isEmpty Method
September 1, 2008 - Learn about the Java String isEmpty method, its syntax, and how to use it to check if a string is empty in your Java applications.
🌐
Codecademy
codecademy.com › docs › java › arraylist › .isempty()
Java | ArrayList | .isEmpty() | Codecademy
March 10, 2024 - The .isEmpty() function checks if a given ArrayList is empty. It returns true if the ArrayList is empty and false if it is not empty. ... Looking for an introduction to the theory behind programming?
🌐
W3Schools
w3schools.com › java › ref_arraylist_isempty.asp
Java ArrayList isEmpty() Method
The isEmpty() method returns true if a list has no items and false otherwise. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, ...
Find elsewhere
🌐
DataFlair
data-flair.training › blogs › java-string-isempty-method
Java String isEmpty() Method with Examples - DataFlair
August 8, 2024 - Tags: isEmpty() method in javaJavajava isEmpty() methodjava string isEmpty() methodjava string isEmpty() method with examplesjava tutorialsLearn Javastring isEmpty() methodstring isEmpty() method in java ... TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.
🌐
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 - isEmpty() vs empty() vs isBlank() vs isNull() Why Java have these many similar functions with same functionality In this detailed article, we will discuss in detail the functions isEmpty(), empty() …
🌐
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.
🌐
Scaler
scaler.com › home › topics › isempty() in java
Java String isEmpty - Scaler Topics
January 4, 2024 - The isEmpty() is an inbuilt method of the String class that is used to check whether is the given String is empty or not.
🌐
W3Schools
w3schools.com › java › ref_hashmap_isempty.asp
Java HashMap isEmpty() Method
The isEmpty() method returns true if the map has no entries and false otherwise. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an ...
🌐
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.
🌐
Educative
educative.io › answers › what-is-queueisempty-in-java
What is queue.isempty() in Java?
The queue.isEmpty() function in Java is used to check if a queue is empty or not. queue.isEmpty() returns true if the queue is empty; otherwise, it returns false · The illustration below shows the visual representation of the queue.isEmpty() ...
🌐
Tutorial Gateway
tutorialgateway.org › java-string-isempty-method
Java String isEmpty Method
March 28, 2025 - The Java isEmpty method is one of the String Methods, which is to check whether the user-specified string is Empty or Not.
🌐
Medium
medium.com › @AlexanderObregon › java-set-isempty-method-explained-16a8a18f4c63
Java Set.isEmpty() Method Explained | Medium
June 23, 2024 - Understanding the isEmpty() method is vital for efficient Java programming. This method allows you to write cleaner and more efficient code by preventing unnecessary operations on empty sets. For instance, instead of blindly iterating over a set or performing complex operations, you can first ...
🌐
W3Resource
w3resource.com › java-tutorial › string › string_isempty.php
Java String: isEmpty Method - w3resource
Java String Methods · charAt Method · codePointAt Method · codePointBefore Method · codePointCount Method · compareTo Method · compareToIgnoreCase Method · concat Method · contains Method · contentEquals Method · copyValueOf Method · endsWith Method · equals Method · equalsIgnoreCase Method · format Method · getBytes Method · getChars Method · hashCode Method · indexOf Method · intern Method · isEmpty Method ·
🌐
W3Schools
w3schools.com › java › ref_linkedlist_isempty.asp
Java LinkedList isEmpty() Method
The isEmpty() method returns true if a list has no items and false otherwise. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › set-isempty-method-in-java-with-examples
Set isEmpty() method in Java with Examples - GeeksforGeeks
December 31, 2018 - The java.util.Set.isEmpty() method is used to check if a Set is empty or not. It returns True if the Set is empty otherwise it returns False. Syntax: boolean isEmpty() Parameters: This method does not take any parameter Return Value: The method ...