🌐
Coderanch
coderanch.com › t › 375744 › java › representing-empty-string
better way of representing an empty string... (Java in General forum at Coderanch)
So what I wrote and that URL only address how the constants defined in a .java file make it into its respective .class file. Once that class is loaded, I guess it's possible that Java interns the Strings in the class's constant pool. To be relavent, I'll say that I would definitely not create a constant named EMPTY_STRING any more than I'd create a constant named EIGHT.
🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › languages › java
Is there an EMPTY_STRING constant, why not? | Tek-Tips
March 3, 2004 - If it is not null, then it's attibutes (ie its length etc) may or not be an empty *string* or not ... OO design means you can overcome this by implementing your own extension of String : public class MyString extends String { public static String EMPTY_STRING = ""; }
🌐
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)
In this case, an empty string will pass this validation constraint. However, if you set the context parameter javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL to true, the value of the managed bean attribute is passed to the Bean Validation runtime as a null value, causing the @NotNull constraint to fail.
🌐
Delft Stack
delftstack.com › home › howto › java › empty string in java
How to Check Empty String in Java | Delft Stack
February 2, 2024 - In Java, there is no constant that shows an empty string, and even the String class does not have such provision.
🌐
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 - If we’re at least on Java 6, then the simplest way to check for an empty string is String#isEmpty:
🌐
Quora
quora.com › When-would-it-be-useful-to-use-an-empty-string-in-Java
When would it be useful to use an empty string in Java? - Quora
Answer (1 of 5): Ok, lots of answers already, but none of them completely elaborating the question… I’m discussing an empty String (i.e. “”) compared to a null value. As others said, you can pass an empty String to a method that is not expecting (= allowing) null values, because the ...
Find elsewhere
🌐
Medium
medium.com › @rahul.tpointtech12 › exploring-the-possibilities-of-empty-string-in-java-f6f2b300ae70
Exploring the Possibilities of Empty String in Java | by Rahul | Medium
March 20, 2025 - An empty string in Java is a string literal with no characters between the double quotes: “”. It is different from null, which signifies the absence of a string object altogether.
🌐
DeepSource
deepsource.com › directory › java › issues › JAVA-P0063
Use `\"\"` instead of `new String()` to create empty strings (JAVA-P0063) ・ Java
Java guarantees that identical string constants will be represented by the same String object. Therefore, you should just use the empty string constant directly.
🌐
Apache Commons
commons.apache.org › proper › commons-lang › apidocs › constant-values.html
Constant Field Values (Apache Commons Lang 3.20.0 API)
Constant Field · Value · public static final String · JAVA_HOME_KEY · "java.home" public static final String · JAVA_IO_TMPDIR_KEY · "java.io.tmpdir" public static final String · USER_DIR_KEY · "user.dir" public static final String · USER_HOME_KEY · "user.home" public static final String ·
🌐
Quora
quora.com › How-can-I-empty-a-String-in-Java
How to empty a String in Java - Quora
Answer (1 of 11): You can't. Strings are immutable. You can just reference a new string object that has 0 characters. So if you have the string [code] String whoIsAwesome = "you"; [/code] you can assign a new String object to your variable like ...
🌐
iO Flood
ioflood.com › blog › empty-string-java
Working with Java Empty String: A Detailed Guide
February 20, 2024 - However, it’s important to note that an empty string is not the same as a null string. An empty string is a string with no characters, while a null string is a string variable that has not been given a value. This distinction is crucial when dealing with string operations in Java.
🌐
Codemia
codemia.io › knowledge-hub › path › why_is_there_no_stringempty_in_java
Why is there no String.Empty in Java?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
GitHub
github.com › mapstruct › mapstruct › issues › 588
Empty constant String result in unmapped target property · Issue #588 · mapstruct/mapstruct
July 2, 2015 - @Mappings( { @Mapping( target = "codeSpace", constant = CodeSpaceConstants.CS_CONSTRUCTION_STANDARD ) }) abstract ConstructionStandardType toConstructionStandardType( String value ); Results in Unmapped target property "Codespace" while expected Mapped to empty String.
Published   Jul 02, 2015
🌐
Quora
quora.com › In-Java-is-there-any-advantage-of-initialing-a-string-using-StringUtils-EMPTY-vs-null
In Java, is there any advantage of initialing a string using StringUtils.EMPTY vs null? - Quora
Answer (1 of 3): String canonicalization using predefined constants such as StringUtils.EMPTY is an attempt, in this case, to save program memory by reducing duplicate strings. There is some value in reducing the memory footprint and Java has ...
🌐
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.