Hi James. I think I can show you my point of view better with a few examples than by responding **at this moment** to your very compelling examples.
The
word precedence does seem to mean Perform the operator with the highest precedence first. But then we observe the following.
I want to consider a different example first.
Which operation is performed first, + | *, and why?
*, because * has higher precedence than + // false
No. In the first example + is performed first. In the second example * is performed first.
Observe:
Therefore, it is NOT true that the operator with the highest precedence is performed first.
What then does precedence mean? How do we determine which operator is performed first?
The ***only clue*** I have is this from The Java Programming Language, co-authored by James Gosling. �Operator precedence is the �stickiness� of operators relative to each other.� 6.10
1. * is stickier than + and | means
* is applied to the nearest operands.
2. + is stickier than | means
+ is applied to the nearest operands.
3. Since the left-hand operand is evaluated before the right-hand operand,
( f(4) + f(5) ) is evaluated before ( f(2) * f(3) )
Therefore, + is performed before *.
----
In the next example, I change + to *. Using +, the result is the same no matter which operand is evaluated first.
k = 2;
postfix ++ is stickier than prefix ++ and *
prefix ++ is stickier than *
Since the left-hand operand is evaluated before the right-hand operand,
(++ k) is evaluated before (k ++)
Now that I understand this, I read JLS 15.7.3, and I see that explicit parentheses and implicit operator precedence are mentioned together, in the section title and in the first sentence.
My best guess is JLS 15.7.3 means precedence determines implicit parentheses.
Marlene
[ March 14, 2004: Message edited by: Marlene Miller ]