Thee operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.
gurpeet singh wrote:
gurpeet singh wrote:
gurpeet singh wrote:i understand that operands are first evaluated from left to right and then operator precedence is applied.
Stephan van Hulst wrote:
gurpeet singh wrote:i understand that operands are first evaluated from left to right and then operator precedence is applied.
No. Operator precedence is always determined first. This is necessary, because you won't even be able to evaluate an operation if you don't know what operands belong to what operators. As a matter of fact, operator precedence is determined at compile time, whereas evaluation is (obviously) done at runtime.
Evaluation simply means that the results of an expression is being computed. This always happens from left to right. I'll explain using your example.
Before the program is run at all, the compiler already determines how this expression needs to be evaluated. You can imagine this as if the compiler inserts parentheses to make it unambiguous what happens exactly:
++a and a++ get parentheses because they bind more tightly to 'a' and 'a' than the + operator does, because they have higher precedence. Then their respective results are passed to the + operator, because + has higher precedence than =.
So after the compiler is done, and the JVM runs the program, all that's left is evaluation. Precedence doesn't play a role anymore, because all the parentheses have already been inserted. Now, the fact that evaluation is done from left to right doesn't really matter for simple expressions. It only matters for conditional expressions or operations that have side effects. a++ is an example of an operation with a side effect, but don't forget about method calls!
Let's take a look at
You'd expect the last expression to evaluate to 5, with x becoming 5 and y becoming 3.
This however is not the case. This expression will evaluate to 4, with x becoming 4 and y becoming 3.
Why? Here's why:
gurpeet singh wrote:as per javadocs operator precedence chart, post increment operators have higher precedence than pre increment , which means a++ will get evaluated first.
Stephan van Hulst wrote:
Let's take a look at
You'd expect the last expression to evaluate to 5, with x becoming 5 and y becoming 3.
This however is not the case. This expression will evaluate to 4, with x becoming 4 and y becoming 3.
Why? Here's why:
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
If the operator is a compound-assignment operator (§15.26.2), then evaluation of the left-hand operand includes both remembering the variable that the left-hand operand denotes and fetching and saving that variable's value for use in the implied binary operation.
Stephan van Hulst wrote:
gurpeet singh wrote:as per javadocs operator precedence chart, post increment operators have higher precedence than pre increment , which means a++ will get evaluated first.
... Since in your example the pre- and post-increment operators don't compete over the same operand (they are both applied to a separate occurrence of 'a') the compiler doesn't even need to compare the two for priority.
Ivana Kilibarda wrote:Looks like that tables about operators precedence just make confusion in this case.
Stephan van Hulst wrote:the precedence rules for pre-increment and pre-decrement operators are there as a logical consequence of how the compiler is built
Thou shalt not try me. Mom 24:7
Agree. I have see things even worse.Jim Venolia wrote:IMHO, things like "y = ++x + x++" should never be done.
Java® is different. At least...is well‑defined in Java® (i ends up as 3). I once tried that on three C compilers and two produced 3; the third gave 4 as the resultI come from a C background, . . . . Java may be different . . .
Thou shalt not try me. Mom 24:7
That behiour is required by the Java® Language Specification, but the behaviour in C isn't strictly deined, or at least it wasn't when I got that code to produe 4. The rules in C may have changed; I don't know.Jim Venolia wrote:In both C and Java i should be 3. You assign it before you increment it.
No, they have to move into the cardboard box after writing that sort of codeThat said, if someone who reported to me wrote that line they would know I strongly disapproved. Quite possibly whilst living in a dishwasher box under an overpass.
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
|