• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is this!!!!!  RSS feed

 
sharjeel afzal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please explain me what is this and why




the output is


a==a
a==b
b==b
b==a
false




My question is about last line why output of



is just false
why not string a==b is not appended with output and why output is false
 
Lester Burnham
Rancher
Posts: 1337
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You assume that the expression

("a == b " + (a==b))

is evaluated, when in fact this is evaluated:

(("a == b " + a)==b)

Reading up on operator precedence will make it clear why that is so.

(In the future, please restrict yourself to single exclamation marks.)
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseAMeaningfulSubjectLine


The above line is appending- "a==b" to String a and then comparing with the String b. So you dont see "a==b". And It should actually give true for a==b, but then due to this "a==b" being added before String a- Its giving false.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you should use the equals method not == for Strings.
 
sharjeel afzal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Lester Burnham
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please read this FAQ.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!