aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes EqualsEx Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "EqualsEx" Watch "EqualsEx" New topic
Author

EqualsEx

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180


OUTPUT : 1 false 2 true false 5 false 6 false

My Doubt is why the string " 3 " ," 4 " in line 1 are not printing and also totally we have 6 boolean tests but why I am getting only 5 boolean literals.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

First i was surprised to see the result .... then i felt funny ..... it happens
check the brackets in the LINE one


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
This will work fine if you change "Line 1" to

System.out.print(" 3 " + (s2 == s3) + " 4 " + (s2 == s4));//Line 1

Dawn.
Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
Ya I replaced . Now I am able to get the Strings " 3" and " 4 " in the output. But still i didn't get the logic in the brackets(). What happens if there is no bracket ???
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

" 3 " + (s2 == s3) + " 4 " + s2
this part of the statement results to
"sfalse4java"----------- which is a string.
Now you are comparing this... it would be like this
("sfalse4java")==s4
Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
Ohhhhhhhhhhhhhhh my god!!.

Day by day I am learning new things and forgetting old things.......
Thank you James.
mohan rajv
Ranch Hand

Joined: Jan 28, 2009
Posts: 30
this s operator precedence..
== operator has less priority than +
and the expressions are evaluated from left to right..
so in Line1 is evaluated as false.
the expression is similar to :
(" 3 " + (s1==s3) + " 4 "+ s2) == s4
the result of this expression is false..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EqualsEx