aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "".equals(x) or x.equals("")
 
Similar Threads
String Doubts
Differnce between == and equals
String
why not overload equals ?
StringBuffer equals() method