1
public class Test {
    public static void main(String[] args) {
        int x = 3;
        int y = ++x * 5 * x--;
        System.out.println("x is " + x);
        System.out.println("y is " + y);
    }
}

The output is:

x is 3
y is 80

But using the rule that post-operators take precedence over pre-operators, shouldn't it be like this:

y = ++x * 5 * 3 (x is 2)
y = 3 * 5 * 3 (x is 3)
y = 45

Instead, this code is acting as if it just evaluated the expression from left-to-right, evaluating the pre-increment before the post-decrement. Why?

John Thompson
  • 1,674
  • 1
  • 20
  • 35
  • 2
    I tend to think of precedence as being like adding brackets. The code you've shown here doesn't demonstrate precedence between those operators. It's effectively ((++x) * 5) * (x--), and for each binary operator, the LHS is evaluated first. – Jon Skeet Jul 29 '16 at 13:48
  • 1
    *using the rule that post-operators take precedence over pre-operators*, no there is no such rule. The only rule, which explains the output is that of left-to-right evaluation of each expression. – Tunaki Jul 29 '16 at 13:48
  • @JonSkeet thank you! Now I get it. – John Thompson Jul 29 '16 at 13:52
  • 1
    @Tunaki: It's somewhat confusing that the Java tutorial *does* have that precedence: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html It's a poorly worded page IMO. – Jon Skeet Jul 29 '16 at 13:55
  • @JonSkeet you should turn your comment into an answer and I'll accept it. The people who have answered so far didn't bother to explain the meaning of operator precedence. – John Thompson Jul 29 '16 at 14:00
  • @JohnPeterThompsonGarcés: I started to, but the question was closed before I could submit. – Jon Skeet Jul 29 '16 at 14:01
  • @JonSkeet oh, yeah, that's unfortunate. Ok, thanks anyway – John Thompson Jul 29 '16 at 14:07
  • 1
    You were probably reading the OCA textbook from Boyarsky because I stumbled upon the exact same question! Turns out, after searching the web a lil, that "There is no explicit operator precedence table in the Java Language Specification. Different tables on the web and in textbooks disagree in some minor ways." link: http://introcs.cs.princeton.edu/java/11precedence/ I do trust Priceton, but I still have some doubts :) I would reopen this question but I don't have privileges :) – moldovean Sep 17 '17 at 14:35
  • @Tunaki:"using the rule that post-operators take precedence over pre-operators, no there is no such rule." - there is a rule as defined in the table 'Operator Precedence' https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html. There is a clear explanation of this rule given by Merka in https://stackoverflow.com/questions/1489489/why-is-this-java-operator-precedence-being-ignored-here. – Zia Jan 09 '18 at 07:38
  • @Jon Skeet: "It's a poorly worded page IMO" - if you say so to mean the same thing like Tunaki then see my comment above. – Zia Jan 09 '18 at 07:43

2 Answers2

1
int y = ++x * 5 * x--;

++x => Increase then evaluate => x == 4
x-- => Evaluate then decrease

So actually it looks like this:

int y = 4 * 5 * 4; // == 80

and because of the decrement operator, your x is 3 at the end.

QBrute
  • 4,405
  • 6
  • 34
  • 40
  • 1
    You're doing the pre-increment before the post-decrement. So, what do people mean when they say that post-operators have higher precedence than pre-operators? – John Thompson Jul 29 '16 at 13:50
  • @JohnPeterThompsonGarcés they mean that post-operators "bind tighter" than pre-operators, so that in determining which operators _that apply to the same operands_ they apply first. Eg in `--x++` the `++` binds tighter and the expression is effectively `--(x++)` (though it doesn't compile). In your example the operands are different (though they are both written as `x`, they represent potentially different values) so the precedence between pre- and post- operators is not significant. – davmac Jul 29 '16 at 14:01
-1

Your problem is y = ++x * 5 * 3 (x is 2) its not (4)*5*3 its (4)*5*4. Pemdas my friend

    int x = 3;
    int y = ++x * 5 * x--;  //x becomes 4 , then its 4*5*4 =80 = y
    System.out.println("x is " + x); // x-- comes into affect and x = 3
    System.out.println("y is " + y);  // y = 80

Although x-- is being evaluated before x++ and everything else. It changes the value of x after the whole expression is evaluated. Unlike ++x when it get evaluated it changes the value of x immediately.

Javant
  • 1,009
  • 10
  • 17
  • 2
    You haven't really explained that at all, to my mind... – Jon Skeet Jul 29 '16 at 13:46
  • 1
    @JonSkeet better, sorry for the lack of evaluation ? – Javant Jul 29 '16 at 13:48
  • 2
    Not really, you've missed the point of the question - which was the OP stating that post-operators are meant to have higher precedence than pre-operators (as per https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html for example) - your answer doesn't address that at all. – Jon Skeet Jul 29 '16 at 13:54