Use org.apache.commons.lang.StringUtils.EMPTY
Use org.apache.commons.lang.StringUtils.EMPTY
String.EMPTY is 12 characters, and "" is two, and they would both be referencing exactly the same instance in memory at runtime. I'm not entirely sure why String.EMPTY would save on compile time, in fact I think it would be the latter.
Especially considering Strings are immutable, it's not like you can first get an empty String, and perform some operations on it - best to use a StringBuilder (or StringBuffer if you want to be thread-safe) and turn that into a String.
Update
From your comment to the question:
What inspired this is actually
TextBox.setText("");
I believe it would be totally legitimate to provide a constant in your appropriate class:
private static final String EMPTY_STRING = "";
And then reference it as in your code as
TextBox.setText(EMPTY_STRING);
As this way at least you are explicit that you want an empty String, rather than you forgot to fill in the String in your IDE or something similar.
String literals are interned by default, so no matter how many times you refer to "" in code, there will only be one empty String object. I don't see any benefit in declaring EMPTY_STRING. Otherwise, you might as well declare ONE, TWO, THREE, FOUR, etc. for integer literals.
Of course, if you want to change the value of EMPTY_STRING later, it's handy to have it in one place ;)
Why on earth would you want a global variable in Java? James Gosling really tried to get rid of them; don't bring them back, please.
Either
0 == possiblyEmptyString.length()
or
possiblyEmptyString.isEmpty() // Java 6 only
are just as clear.
No, there isn't. But since the entire world (well, almost) uses Jakarta Commons-Lang, you can, too. Use StringUtils.EMPTY: http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringUtils.html#EMPTY
What's wrong with ""? It behaves like a constant, it's taken from the string pool and it's really short to write, much shorter than String.EMPTY.
Also, if what you need is to test if a string is empty, just do this:
myString.isEmpty();
UPDATE
If the string can be null, better use either isEmpty() or isNotBlank() from Apache Common's StringUtils class.
Of course not. Do you really think "" is not clear enough ?
Constants have essentially 3 use cases:
- Document the meaning of a value (with constant name + javadoc)
- Synchronize clients on a common value.
- Provide a shortcut to a special value to avoid some init costs
None apply here.
I use StringUtils.EMPTY, for hiding the literal and also to express that return StringUtils.EMPTY was fully expected and there should return an empty string, "" can lead to the assumption that "" can be easily changed into something else and that this was maybe only a mistake. I think the EMPTY is more expressive.
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