• Post Reply Bookmark Topic Watch Topic
  • New Topic

Wierd output using Strings in SOP??  RSS feed

 
Karthikayini Devi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is my simple program:



And the output is:

s1==s2
true
false

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???

Thanks,
Srika.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().

Winston
 
Karthikayini Devi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Winston, I went through the post. I agree not to use double equals for comparing objects.

But why the third SOP didn't print the string inside the quotes?

Thanks,
Srika.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Winston said you should never use == to compare Strings(or for that matter any Object). you should always use equals(). the == operator is for referential equality. it return true only if the references refer to same object. the first SOP output is simple. the second returns true, which means the references s1, s2 point to same object. this is also expected because of string pool . there is only one string object with the value "abc" and the two refernces are merely referencing it. now the third one is based on operator precedence. if you look at the operator precedence chart, you will find that + operator has HIGHER PRECEDENCE than == . So it will be executed first resulting in string concatenation, which gets compared with "abc", which as you might have guessed at this point of time will return false.
 
Karthikayini Devi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks,
Srika
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks,
Srika


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
 
Karthikayini Devi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thank you Gurpreet. I understood now.

When I added a parenthesis, I got the desired result "s1==s2false"

Thanks,
Srika.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthikayini Devi wrote:Hey, thank you Gurpreet. I understood now.

When I added a parenthesis, I got the desired result "s1==s2false"

Thanks,
Srika.


if you added parenthesis, you should get s1==s2true.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:since + has higher precedence "s1==s2" + s1 will be evaluated. this will result in concatenation to produces trueabc .

Not quite. The comparison inside "s1==s2" will not be evaluated; the result of "s1==s2" + s1 will be "s1==s2abc".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!