Originally Posted by
fredyeboah
Hi Z...Do you mean to say that Java evaluate expression from left-to-right irrespective of the operator precedence?.
I did not say that, and I certainly did not mean to say that. Please re-read the quoted section from the Java Language Specification. Here. I'll quote it again:
The left-hand operand of a binary operator appears to be fully evaluated before
any part of the right-hand operand is evaluated.
Is there any question as to what that means? It has nothing (really: absolutely
nothing) do do with operator precedence. Period. Full stop.
I can't think of a way to "explain" the sentence that I just re-quoted, but I will reword it a little less precisely but more specifically:
For an expression like "y = x + AnythingatAtAll" compiler plugs in the current value of x before it evaluates any part of the stuff on the right-hand side of the binary '+' operator. Period. Full stop.
Did you see the next sentence in my quote from the previous post:
It is recommended that code not rely crucially on this specification. Code is
usually clearer when each expression contains at most one side effect, as its
outermost operation, and when code does not depend on exactly which exception
arises as a consequence of the left-to-right evaluation of expressions.
In other words: They recommend that you do not write expressions that depend on whether the evaluation is left-to-right or right-to-left. That's not just my recommendation. That's the recommendation of the Guys Who Wrote The Book.
So, for example,don't write code with stuff like "y = x + ++x or "y = x++ + ++x" or even "x = ++x'. These are all legal Java expressions, and the compiler will perform the operations in a prescribed manner, but we mere humans might get caught up in a frivolous debate about what we "think" it should do. Etc., etc., etc...
I mean, I tried to give an explanation consistent with the Java Language Specification (and consistent with the results from my test program that used that expression). It's not a Bad Thing to try to understand why it works the way it works, but I can't see much chance of my being able to explain why it doesn't work some other way that someone "thought" it should work.
I'm thinking that the reason the authors of the Java Language Specification felt the need to dis-recommend such expressions is because some people get confused because they don't know about (or can't understand) the simple declarative statement, "The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated."
Now, the reason that your original expression (as corrected in post #3 of this thread) depends on the left-to-right thing is that the variable x appears twice and there is an operation that has a side effect on x. Bottom line: For best understanding and code maintainability, it is recommended that you not put expressions like that in your code. (If a term involves a side effect on a variable, don't let that variable appear more than once in the expression.) Period. Full Stop.
Originally Posted by
fredyeboah
...what is the essence of (++) having a higher precedence over (+)?
For the expression "x + ++x", after the code has plugged in the original value of x into its expression evaluator (because x is on the left-hand side of the binary operator '+'), it looks at the right-hand side and decides what to do. The right-hand side of the binary '+' operator is '++x.' The code will increment the value of x in memory and then perform the '+' operation, adding the incremented value to the previous expression value.
Originally Posted by
fredyeboah
Under what circumstance does the compiler make its choices?
When the code gets around to evaluating any part of an expression, it applies rules of precedence.
Another quote from the Java Language Specification:
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.
Example 1:
If the expression is y = a + b*c, the code plugs in the value of a, then it looks at the right-hand side of the '+' operator. It sees "b*c" and has to know what to do next. Since the '*' operator has higher precedence than '+', the multiplication is performed and the product is added to the previous value of the expression.
Example 2:
If the expression is y = a * b + c, the code plugs in the value of a and then looks at the right-hand side of the '*' operator. It sees "b+c". Since the '+' operator has lower precedence than '*', the previous value of the expression is multiplied by b. Then the compiler looks on the right-hand side of the '+' operator and sees "c" It adds the value of c to the previous value of the expression.
That's how precedence rules are applied.
A final note: If you have way to examine machine language output from a compiler, you might see a sequence of operations that look a little different (or a lot different, depending on the compiler's optimization capabilities), but the code is required to perform as described in the Java Language Specification. That is the import of the word "appears" in the sentence "The left-hand operand of a binary operator
appears to be fully evaluated..." The code can do anything it wants to as long as it satisfies the specified behavior.
Cheers!
Z