This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark """.equals(x)  or  x.equals("") " Watch """.equals(x)  or  x.equals("") " New topic

"".equals(x) or x.equals("")

Gail Mikels
Ranch Hand

Joined: May 07, 2001
Posts: 634
I've seen code written both ways - is there an advantage to doing it one way vs the other?


Gail Mikels
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

The first way is safer - myString could be null.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Gail Mikels
Ranch Hand

Joined: May 07, 2001
Posts: 634
Ahh.. good point. I guess this would eliminate all the ...

if(myString != null && myString.equals("blah..."))

Cool. Thanks!!
I agree. Here's the link:
subject: "".equals(x) or x.equals("")
It's not a secret anymore!