• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gail Schlentz
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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")


 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first way is safer - myString could be null.
 
Gail Schlentz
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh.. good point. I guess this would eliminate all the ...

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

Cool. Thanks!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic