Precedence of operators

Asked

Viewed 527 times

11

I have a question regarding the precedence of operators in Java. I have the following code:

int x = 2;
long y = 1 + x * 4 - ++x; 

Viewing the Precedence Table here.

In my view the expression should be resolved as follows:

1 + x * 4 - 3
1 + 3 * 4 - 3
1 + 12 - 3
y=10

Contrary to what I have said, the answer given by the implementing programme is y=6. I wonder why he performed the multiplication before the increment?

By the precedence table, increment in my view should be performed first.

3 answers

9


Your mistake was to assume that the sub-expression ++x would be evaluated before of sub-expression x * 4. At the time this expression is evaluated, x still worth 2, so that the correct resolution is:

(1 + (x * 4)) - (++x)   [x=2]
(1 + (2 * 4)) - (++x)   [x=2]
(1 + 8) - (++x)         [x=2]
9 - (++x)               [x=2]
9 - x                   [x=3]
9 - 3                   [x=3]
6                       [x=3]

Operator precedence refers only to "where to place parentheses" in the expression. Evaluation, however, occurs from left to right, just like everything else in the programming language (e.g.: order of assessment of the arguments of a function, order of execution of the instructions, etc).

2

Because there is also the associativity that is left to right. It does not analyze all operators and chooses which one will run first. He does this with the operands, usually in pairs, from left to right in most cases. Only changes direction when associativity is reversed as is the case, for example, of assignment operators.

Then the multiplication is not mixing with the increment operation, it is executed before taking notice that there will be this operation.

Table of precedence with associativity.

The ideal is to avoid using operators that cause side effects in larger expressions, usually the increment operator is best used alone or when its isolation becomes clear.

  • Even if the sum and subtraction operators associated to the right, the relative execution order between x*4 and ++x would still be the same. 1 + ((x * 4) - (++x)). And in this particular case, even the result would be the same (it would not be the case if the first operator were a -)

  • Yes, in this case yes, but what he is imagining is that there is no associativity and the expression is evaluated as a whole. He thinks that because the increment is precedent he should be evaluated first even being on the right, and then go to the other operators.

  • 1

    I think I know what you mean. In fact, an expression without side effects could be evaluated in the order "leaves first, rising toward the root" (i.e. most previous operations being applied before the least preceding), and the result would be the same as "left branch first, right branch after". With side effects, the results are different. But note that this is an arbitrary convention imposed by the programming language, because the way to include parentheses according to precedence and associativity would be the same in both cases.

  • Yes, I agree..

0

The expression is left to right. So:

int x = 2;
long y = 1 + x * 4 - ++x;
1 + 2 * 4 - ++x;
1 + 8 - ++2;
1 + 8 - 3;
9 - 3;
6

Browser other questions tagged

You are not signed in. Login or sign up in order to post.