Meaningless Drivel is fun!*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark """.equals(x)  or  x.equals("") " Watch """.equals(x)  or  x.equals("") " New topic
Author

"".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?

"hello".equals(myString)
or
myString.equals("hello")




Gail Mikels
Paul Sturrock
Bartender

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!!
 
 
subject: "".equals(x) or x.equals("")
 
Similar Threads
StringBuffer equals() method
String
String Doubts
why not overload equals ?
Differnce between == and equals