0

I have the following code in Java :

int a=5,b=10,c;
c = ++b * ++a - a++ ;
System.out.println(c);

Now referring to the Java operators precedence table :-

Table

We will first evaluate the postfix a++ and then prefix ++a and ++b, which gives us :

c= 11 * 7 - 5 = 72

But this answer is wrong, and the correct answer is 60 What am I doing here?

  • Why should `a=5` and then `++a` give you `7`? Also by the time you reach `a++`, it isn't `5` anymore. The whole expression evaluates to `11 * 6 - 6 = 60` – QBrute Oct 24 '22 at 08:41
  • 1
    Does this answer your question? [How do the post increment (i++) and pre increment (++i) operators work in Java?](https://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java) – QBrute Oct 24 '22 at 08:43
  • Unary operators have residence `++b` results to `11` and `++a` would give `6`. Postincrement `a++` would change **only** the value stored in variable `a` to `7`, but while calculating `c` the value `6` would be used. Therefore, you would have `c = 11 * 6 - 6;` – Alexander Ivanchenko Oct 24 '22 at 08:43
  • hey @QBrute, when a++ is executed, we will first utilize the value and then increase it by 1 , so value of a becomes 6, but in place of a++ we write 5 only. Now ++a means pre-increment so we will first increase the value and then utilize it, so ++a becomes 6+1 = 7. – Anshul V. Kumar Oct 24 '22 at 08:44
  • I know how the operators work, your explanation is wrong. By the time you reach `a++`, the value of `a` is already `6` because of the preceding `++a`. So you have `11 * 6 - 6 = 60` which is the correct answer that you're referring to... – QBrute Oct 24 '22 at 08:47
  • @QBrute I get what you're saying, but Java docs say post increment has higher precedence so shouldn't a++ be executed earlier than ++a? – Anshul V. Kumar Oct 24 '22 at 08:48
  • @AnshulV.Kumar Precedence between pre- and postincrement doesn't come into play here, since they are not applied to the same operand. – Konrad Rudolph Oct 24 '22 at 08:50
  • No, because Java still evaluates from left to right and follows mathematical rules. The left expression is a multiplication, so `++b * ++a` is evaluated first, which in turn increases `a` before you reach the `- a++` part – QBrute Oct 24 '22 at 08:50

2 Answers2

2

According to the Java Language Specification section 15.7.1,

The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

This applies, in your expression, to the - operator. That means that ++b * ++a is fully evaluated before any part of a++ is evaluated.

When ++b * ++a is evaluated, a side-effect is that a is increased to 6. The value of ++b * ++a is 66, because with a prefix operator, the "post-operation" value of the variable is used in the calculation, so this is 11 * 6.

Once ++b * ++a has been evaluated, the right hand operand of the - operation can be evaluated. This is a++, which evaluates to 6, but increases a to 7.

The subtraction is therefore 66 - 6, which is 60.

Unfortunately, you have been misled by the table in the Oracle Java tutorial, which suggests that postfix operators are always evaluated before prefix operators. Since this statement contradicts the Java Language Specification, I can only imagine that the authors of the Oracle Java tutorial actually intended something different, but didn't express themselves very well.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Thanks so much! This was my exact problem. I thought I was going crazy lol. Now it makes sense! – Anshul V. Kumar Oct 24 '22 at 09:03
  • 1
    In general, you should mistrust every precedence table for Java. As [the specification](https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.2:~:text=Precedence%20among%20operators%20is%20managed%20by%20a%20hierarchy%20of%20grammar%20productions.) says: “*Precedence among operators is managed by a hierarchy of grammar productions.*”. And the rules implied by the grammar are more complicated than a simple list or table suggests. – Holger Oct 24 '22 at 13:18
1

The difference between ++a and a++ is that in first it will increase the value of a and then use it's result as the input for next expression but a++ will use the value of a for the expression and then increase a.

Also precedence of multiplication is higher than addition/substraction. In your case:

c = ++b * ++a - a++ ;
c = ((++10) * (++5)) - (a++)
c = (11 * 6) - (6++) // a's value is 6 because of increment.
c = 66 - 6 // A value will become 7 but 6 will be used.
c = 60
Just a Person
  • 1,276
  • 1
  • 5
  • 23
  • No, that's not OP's problem. OP's problem is that they have been misled by the Java tutorial to believe that postfix operators are evaluated before prefix operators - a statement that contradicts the Java Language Specification. – Dawood ibn Kareem Oct 24 '22 at 08:53
  • No, postfix and prefix operators don't come into play because multiplication gets higher precedence than subtraction. – Just a Person Oct 24 '22 at 08:54
  • No, I believe you are mistaken. OP has specifically quoted the table which shows the precedence of the postfix and prefix operators, and explained how that table has misled them about that these operators would be evaluated in. The relative precedence of multiplication and subtraction is nothing at all to do with why they were confused. – Dawood ibn Kareem Oct 24 '22 at 09:02