• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Wierd output using Strings in SOP??

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic