This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Behavior of equal operator(==) in SOP

 
arfeen khan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rancher's,

During my test program i found a weird behavior for equal operator .

The Problem is:
If i try to print the return value of "equal operator" for two reference variable pointing to same string object.
the result always comes FALSE.

Following is the code snippet--


Actual Result is:

inside if -- Got true for sr1==sr2
false
End of Program.


Expected Result:
inside if -- Got true for sr1==sr2
The value of sr1==sr2 [true].
End of Program.


The red marked line should come.

Can any Rancher, please suggest why this happening.

Thanks,
Arfeen.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you put:

you'll get the result you expected. The thing is you have to take care you understand operator precedence and associativity to get into more details. Simply put, what your line does:

is actually first concatenating "The value of sr1==sr2 [" and "Arfeen" and then compare it as "The value of sr1==sr2 [Arfeen" with other string because + operator is of higher precedence than == operator (it's also left associative). Hence, you only get the result of that comparison, which is false.

I hope I was clear, if you have any more doubts feel free to ask.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic