Karthikayini Devi wrote:What is going on here? First time it prints true for the comparison, and next time, it prints false?? And Whatever inside the quotes in third SOP is also not printed out!! Why???
Probably the #1 topic on the Beginners forum, and I blame the SCJP exams.
You might want to have a look at the most recent thread, but the fact is that you should almost NEVER use '==' when comparing Strings (or any other object type). Use equals().
But why the third SOP didn't print the string inside the quotes?
- X 2
Karthikayini Devi wrote:Gurpreet, if string concatenation happens first, where is the concatenated string? Why it prints only false? It should print "s1==s2false" right?
Could you kindly explain me little elaborately?
the expression is "s1==s2" + s1==s2
since + has higher precedence "s1==s2" + s1 will be evaluated. this will result in concatenation to produces trueabc . this will be compared to s2(which points to string with value abc). here since the objects are different , the == operator will return false. did you get it ? if you still have any doubt , feel free to ask