Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling equals method on String

 
shashi kir
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a String s="apple"
What is the difference between s.equals("apple") and "apple".equals(s)? and which is better? Please post the correct answer
 
Campbell Ritchie
Marshal
Posts: 53110
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends which language you are using. Both are valid syntax in Java, but they mean different things. I think only the one with = is valid SQL syntax, but you should check whether you want "apple" or 'apple' in SQL.

If you try passing s in Java and s is null, then s.equals... will throw a NullPointerException and "apple".equals(s) will evaluate to the value false.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!