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.
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
posted
0
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
posted
0
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.