1

I understand the difference between this operations (post-increment, pre-increment). But question: I have an expression:

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

The highest priority has post-unary operator, than "*" and last "-". In my opinion it will be:

long y = x * 4 - x++;

1). x++ => return 4 (save x = 5)
2). final expression: 5 * 4 - 4 = 16

But when I compile this in IDE the answer is 12 ! What's a problem and where did I do smth. wrong?

Andronicus
  • 25,419
  • 17
  • 47
  • 88
ctrlaltdel
  • 145
  • 1
  • 2
  • 7
  • `x * 4` is evaluated first, which is 16. Then it evaluates `x++` which first gets `x` and then increases `x`. At the time of getting it is 4. so 16 - 4 = 12. If you were to change it to `++x`, then the answer would be 11. – Neijwiert Mar 15 '19 at 07:58
  • Possible duplicate of [When are values in Java variables evaluated/returned/fetched in an expression?](https://stackoverflow.com/questions/39925647/when-are-values-in-java-variables-evaluated-returned-fetched-in-an-expression) – Januson Mar 15 '19 at 08:00
  • No, is not the same. Priority of "+" and "*" quite different – ctrlaltdel Mar 15 '19 at 08:14
  • I don't understand WHY "*" is first?? [oracle site](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) – ctrlaltdel Mar 15 '19 at 08:15
  • expr++ has the hieghts priority!! look at the example: int y = ++x * 5 / x-- + --x; the result will be 7 (if x = 3) – ctrlaltdel Mar 15 '19 at 08:16

5 Answers5

3
long y = 4 * 4 - 4;

x will be incremented after this assignment

Everytime x is "called" with x++ it gets incremented after E.g.

    int x = 1;
    System.out.println(x++ + x++ * x++ + x++);
    // 1 + 2 * 3 + 4
ave4496
  • 2,950
  • 3
  • 21
  • 48
3

EDITED

Here are the steps to evaluate the expression:

  1. Evaluate all the expression that has increment/decrement from left to right
  2. Replace all variable with their true value
  3. Evaluate the expression using PEMDAS rule

Example:

int x = 4;   
int y = ++x * 5 / x-- + --x;  
  • first we need to substitute all values of x before evaluating the expression (substitute from left to right)
    ++x -> since it is a post increment we will first increment x before substituting, thus x will be 5
    5 * 5 / x-- + --x -> this will be the new equation
    now we will substitute x in x--
    x-- -> since it is a post decrement x here will be substituted with 5 and after the substitution decrement x, thus x will be 4
    5 * 5 / 5 + --x -> this will be the new equation
    now we will substitute x in --x
    --x -> since it is a pre decrement we will first decrement x substituting, thus x will be 3
    5 * 5 / 5 + 3 // this will be the new equation

  • Since there are no variables in the equation we will now evaluate the expression using PEMDAS
    25 / 5 + 3 5 + 3
    8
    thus the result will be 8

Mark Melgo
  • 1,477
  • 1
  • 13
  • 30
  • 1
    OK, but why this information in [of. oracle site](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) ? – ctrlaltdel Mar 15 '19 at 08:18
  • 1
    It is basic math. It is not a Java only thing. – Mark Melgo Mar 15 '19 at 08:21
  • OK, basic maths. But why the result of long y = - x++ + x * 4; is 13? By your words, the multiplication must be the first! But no, (x++) the first – ctrlaltdel Mar 15 '19 at 08:27
  • And this: int y = ++x * 5 / x-- + --x; result 7. It means, that all post/pre operators executed first – ctrlaltdel Mar 15 '19 at 08:34
  • 1
    ```long y = -x++ + x * 4;``` will be 16. My explanation is incorrect. MDAS only applies to those without the increment/decrement. I have updated my explanation. – Mark Melgo Mar 15 '19 at 08:36
  • ```int y = ++x * 5 / x-- + --x;``` will not result to 7, it will result to 8, it will be ```5 * 5 / 5 + 3;`` – Mark Melgo Mar 15 '19 at 08:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190070/discussion-between-mark-and-ctrlaltdel). – Mark Melgo Mar 15 '19 at 08:38
  • I have only followed your question which is ```int x = 4;``` but you changed it to ```int x = 3;``` – Mark Melgo Mar 15 '19 at 08:39
  • I don't understand your explanation. "since Java executes equations from left to right" is strange. How about rules of maths and priority? In your case, the expression 2 + 2 * 2 will be 8 because Java executes from left to right – ctrlaltdel Mar 15 '19 at 08:42
  • 1
    @Mark - I think that this is a more detailed and better operator precedence chart than the oracle chart given by the OP - https://introcs.cs.princeton.edu/java/11precedence/ – MasterJoe Mar 13 '20 at 20:35
2

x evaluates to 4, and x++ evaluates to 4 as well, and then gets incremented to 5. So it's essentially 4 * 4 - 4 , which gives 12 as expected.

nullPointer
  • 4,419
  • 1
  • 15
  • 27
1

Multiplication is resolved separately, so x == 4. Incrementation occurs afterwards, so it's actually 4 * 4 - 4 == 12 and after this operation x == 5.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
1

The left side of a subtraction is always computed before the right side because subtraction is left associative i.e. 5 - 2 - 1 is (5 - 2) - 1, not 5 - (2 - 1).

This is why the multiplication happens before the increment. Associativity determines what happens first here, not precedence.

Leo Aso
  • 11,898
  • 3
  • 25
  • 46