How do I compare strings in Java?
I've always used == to compare strings until now. I ran into an error, so I decided to change one of the equals signs to .equals() and this solved my problem. Why didn't == work? Are there cases where == should not be used and .equals() should be used instead (obviously aside from fixing the bug.) I'm not sure I really see a difference between the two operators. What's the difference?
== is a reference, values comparison, i.e. both objects point to the same memory location.
.equals() -> evaluates to the comparison of values in the objects.
You have two options
1- use equals method
2- equals to
Both are used for string matching
by Grace Marguerite Williams2 years ago
What makes so many people compare the sociopolitical beliefs of Donald Trump to that ofAdolf Hitler? Are such comparison far-fetched or valid? Why? Why not? There are those very fearful that Donald Trump is...
by John Connor4 years ago
Please compare and contrast Adolph Hitler with Barack Hussein Obama.I am interested in discerning more about Barack Obama's political agenda. I am unable to decide if he is similar or significantly different than Adolph...
by Zsuzsy Bee8 years ago
Just in the name of fun!!!!!! While I was trying to clean up some stuff off my computer I came across these. A couple of them made me smile even though they're probably as old as the hills. So it being Friday morning...
by Rui Carreira5 years ago
What is the difference between God and a unicorn?Why don't people believe in Dragons if they believe in God? What's the difference here? I'm not an atheist, I'm just curious.
by Richa2 years ago
I think' Love has no reason and also no comparison, if u practice both then that's pity" R U Agree?Those who give reason to love someone and compare his/ her love with others are misguided people who does not...
Copyright © 2018 HubPages Inc. and respective owners.
Other product and company names shown may be trademarks of their respective owners.
HubPages® is a registered Service Mark of HubPages, Inc.
HubPages and Hubbers (authors) may earn revenue on this page based on affiliate relationships and advertisements with partners including Amazon, Google, and others.