Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDK constant for empty String [duplicate]

Tags:

java

Possible Duplicate:
Why is there no String.Empty in java?

Is there a JDK constant for an empty String, something like String.EMPTY, equaling ""? I can't seem to find it. I would prefer using that rather than "".

Thanks

like image 357
amphibient Avatar asked Dec 04 '22 14:12

amphibient


2 Answers

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

like image 66
Isaac Avatar answered Dec 31 '22 10:12

Isaac


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.

like image 41
Óscar López Avatar answered Dec 31 '22 11:12

Óscar López