Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
difference between "john".equals(str) and str.equals("john")
ShaikSalam Bashoeb
Greenhorn
Posts: 24
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
HI. May i know the difference between
"john".equals(str)
and
str.equals("john")
What I heard is Better approach is the First Code.
Any Suggestions will be appreciated................
David O'Meara
Rancher
Posts: 13459
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
the first will return 'false' if str is null in the first case, it will throw a NullPointerException in the second
ShaikSalam Bashoeb
Greenhorn
Posts: 24
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Millions of Thanks David
Rob Spoor
Sheriff
Posts: 22783
131
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
"john".equals(str)
is more or less similar to
str != null && str.equals("john")
And that extra part of the condition is exactly why several programmers, me included, prefer calling equals on the
String
constant, not the variable.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
David O'Meara
Rancher
Posts: 13459
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
and in practice it is better to call .equals() on the known part and not jsu for Strings. Keep this in mind for Integers and other types.
Looky! I'm being abducted by space aliens! Me and this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JButton not working
Please explain the output
hashCode Question
String intern();
Incorrect equals() implementation...
More...