The following code prints out \"3\", not \"4\" as you might expect.
public class Foo2 {
public static void main(String[] args) {
int a=1, b=2;
I had the same problem with this operator precedence definition (as defined here) and I think none of the above replies are exactly explaining and clarifying the paradox in this definition. This is what I think the higher precedence of postfix operator to other operators (in this example to binary plus operator) means.
Consider the following code fragments:
int x = 1, y =4 , z;
z = x+++y; // evaluates as: x++ + y
System.out.println("z : " + z); // z: 5
System.out.println("y : " + y); // y: 4
System.out.println("x : " + x); // x: 2
x = 1; y =4 ;
z = x + ++y;
System.out.println("z : " + z); // z: 6
System.out.println("y : " + y); // y: 5
System.out.println("x : " + x); // x: 1
As you can see, a single expression z = x+++y;
which has two possible evaluations, will be evaluated as z = x++ + y;
by java compiler. This means that from three plus sign which came together, compiler assumes the first two of them as a postfix operator and the third one as a binary plus operator. This is in fact a result of higher precedence of postfix operator over other operators.
The second code fragment shows how the outputs differ by writing the expression as z = x + ++y;
which explicitly specifies which plus sign is a binary operator.