This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes doubt in jls - order of evaluation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "doubt in jls - order of evaluation" Watch "doubt in jls - order of evaluation" New topic
Author

doubt in jls - order of evaluation

mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
jls
15.7 Evaluation Order
The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.
....
15.7.3 Evaluation Respects Parentheses and Precedence
Java programming language implementations must respect the order of evaluation as indicated explicitly by parentheses and implicitly by operator precedence.
My question is that these seem a bit contradictory to me. If evaluation order is always from left to right, then how can it be affected by parentheses or operator precedence? I can understand that the actual grouping of expression depend on operator associativity, and may be changed by using parentheses.
for eg a*b*c is same as (a*b)*c and not a*(b*c). But this is different from order of evaluation. For eg.
x = f1()+f2()*f3(); results in f1() invoked before f2() and
f3(), which supports 15.7 above, that the order is always from left to right.
Any views?
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
I think the differnce in the examples is that the operands are first resolved (which is done left to right) and then the operator precedences kick in.
This is the test I used

Sean
[This message has been edited by Sean MacLean (edited November 09, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
The key is that 15.7 is talking about the operands, while section 15.7.3 is talking about the operators. A subtle point, but very important to following the JLS rules here.
In the expression
f1() + f2() * f3()
the operators are "+" and "*". By precedence "*" must be evaluated before "+", according to 15.7.3. It has operands "f2()" and "f3()" - according to 15.7.1, "f2()" must be evaluated first, and then "f3()".
The "+" operator, on the other hand, has operands "f1()" and "f2() * f3()". (This results from the parsing rules given in 15.17 and 15.18.) Of these two operands, "f1()" is evaluated before "f2() * f3()", again according to 15.7.1.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in jls - order of evaluation
 
Similar Threads
Q about '|| &&'
Parentheses in Expression
Order of precedence & evaluation
Gist/Notes on Operator Precedence and Order Evaluation
doubt in jls - order of Evaluation