File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator Precedence 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 "Operator Precedence" Watch "Operator Precedence" New topic
Author

Operator Precedence

Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi Ranchers,
Pls go thru the following code:

Pls explain the o/p of the above code.
i thought the o/p will be 1,2,3,4,3.But its not correct.
Pls expalin in detail.
Regards
Vineela
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Vineela,
1.) every call to the m method is evaluated,
this results in:
m( 1 + 2 % 3 * 4 )
2.) the expression is evaluated from left
to right and following the precendence
rules which looks like this with brackets:
m1(1 + ((2 % 3) * 4))
and results in:
m1(9)
the whole thing gives us: 1,2,3,4,9,
HTH, greetings,
Gian Franco


"Eppur si muove!"
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi Gian,
But A/c to Operator Precedence '*' is having highest precedence than '%'.So, i think the exp should be evaluated as
1+(2%(3*4)) resulting in a value of 3.
Am i going wrong anywhere?
Pls correct me.
Regards
Vineela
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Vineela,
the operators * and % have the same precedence and
it depends on which one comes first when read from
left to right. So the following:
m(m(1) + m(4) * m(2) % m(3));

gives another result than
m(m(1) + m(2) % m(3) * m(4));

HTH, greetings,
Gian Franco
seema pujari
Greenhorn

Joined: Apr 02, 2004
Posts: 1
Hi Vineela,
*, / and % have the same precedence. They associate from left to right.
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi Gian,
Thanku.I got it.
Regards
Vineela
 
wood burning stoves
 
subject: Operator Precedence