views:

143

answers:

5

I've been taking a look at some GWT code written by various people and there are different ways of comparing strings. I'm curious if this is just a style choice, or if one is more optimized than another:

"".equals(myString);

myString.equals("");

myString.isEmpty();

Is there a difference?

+13  A: 
"".equals(myString);

will not throw a NullPointerException if myString is null. That is why a lot of developers use this form.

myString.isEmpty();

is the best way if myString is never null, because it explains what is going on. The compiler may optimize this or myString.equals(""), so it is more of a style choice. isEmpty() shows your intent better than equals(""), so it is generally preferred.

Aaron
You're correct about "".equals(myString), of course, but I find that this intentionally backward syntax hurts my eyes. I think that if your code runs into a situation where you can't be sure if a String value isn't null, chances are there's an underlying logic problem being plastered over.
Carl Smotricz
@Carl I find the double quotes outside of the parenthesis look better. But absolutely say NPE to nulls early and often.
Tom Hawtin - tackline
isEmpty() is much more efficient as it only test the string length against 0, while equals involves casting, comparing hash codes and so on. See http://www.docjar.com/docs/api/java/lang/String.html
Helper Method
A: 

Both of the options using "" may require the creation of a temporary String object but the .isEmpty() function shouldn't.

If they bothered to put the .isEmpty() function in I say it is probably best to use it!

roguenut
I'd be very surprised if the empty string constant wasn't already part of any JRE's intern pool shortly after startup. And once such a constant is used anywhere within a classloader, it stays available until the JVM exits, so I wouldn't be too concerned about a performance hit.
Carl Smotricz
isEmpty() was added as a convenience method. Before that, you'd had to check the String length against null like this: if (s.length() == 0).
Helper Method
+2  A: 

apache StringUtils provides some convenience methods for, well, String manipulation.

http://commons.apache.org/lang/api/org/apache/commons/lang/StringUtils.html#isBlank(java.lang.CharSequence)

check out that method and associated ones.

hvgotcodes
Is `isEmpty` not enough for you?
Tom Hawtin - tackline
why'd vote me down brah? I mean, the advantage of the StringUtils is that you add consistency to your code so you dont have "".equals(blah) vs blah.equals("").
hvgotcodes
@Tom Well, `StringUtils` methods are null safe.
Pascal Thivent
right on, i dont see why i got a downvote for suggesting an api that is well known, respected, and provides some consistency to these things, simple though they are :(
hvgotcodes
@hvgotcodes this is how SO works, stupid people have the same voting power as smart people
unbeli
@unbeli lol. thanx for the support...;)
hvgotcodes
+5  A: 

Beware that isEmpty() was added in Java 6, and, unfortunately, there are still people who complain pretty loudly if you don't support Java 1.4.

erickson
very good to know.
KevMo
A: 

myString.isEmpty() is probably best if you are working on a recent version of Java (1.6). It is likely to perform better than myString.equals("") as it only needs to examine one string.

"".equals(myString) has the property of not throwing a null pointer exception if myString is null. However for that reason alone I'd avoid it as it is usually better to fail fast if you hit an unexpected condition. Otherwise some little bug in the future will be very difficult to track down.....

myString.equals("") is the most natural / idiomatic approach for people wanting to keep compatibility with older Java versions, or who just want to be very explicit about what they are comparing to.

mikera