In Java, which gets executed first, "+" or "++"?
JavaOperator PrecedenceJava Problem Overview
I tried the following code in Java
t1 = 5;
t2 = t1 + (++t1);
System.out.println (t2);
My view is since ++ has a higher precedence than +, the above becomes
t2 = t1 + (++t1);
t2 = t1 + 6; // t1 becomes 6 here
t2 = 6 + 6;
t2 = 12;
However, I get the answer 11 for t2. Can someone explain?
Java Solutions
Solution 1 - Java
You are nearly correct but you are subtly misunderstanding how the precedence rules work.
Compare these two cases:
int t1 = 5;
int t2 = t1 + (++t1);
System.out.println (t2);
t1 = 5;
t2 = (++t1) + t1;
System.out.println (t2);
The result is:
11
12
The precedence does indeed say to evaluate the ++
before the +
, but that doesn't apply until it reaches that part of the expression.
Your expression is of the form X + Y
Where X
is t1
and Y
is (++t1)
The left branch, i.e. X
, is evaluated first.
Afterwards the right branch, i.e. Y
, is evaluated.
Only when it comes to evaluate Y
the ++
operation is performed.
The precedence rules only say that the ++
is "inside" the Y
expression, they don't say anything about the order of operations.
Solution 2 - Java
Your logic is close, but not quite right. The order of evaluation is Left to Right for the + operator. t1 comes before the binary op, LHS and then the increment is on the RHS of that binary op. The LHS is executed first.
t2 = t1 + (++t1);
t2 = 5 + 6; // t1 becomes 6 here as a side effect before being read on the RHS
t2 = 11;
Visualised as a tree you have,
+
/ \
t1 ++t1
Precedence Order
When two operators share an operand the operator with the higher precedence goes first. For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3 since multiplication has a higher precedence than addition.
Associativity
When two operators with the same precedence the expression is evaluated according to its associativity. For example x = y = z = 17 is treated as x = (y = (z = 17)), leaving all three variables with the value 17, since the = operator has right-to-left associativity (and an assignment statement evaluates to the value on the right hand side). On the other hand, 72 / 2 / 3 is treated as (72 / 2) / 3 since the / operator has left-to-right associativity.
Solution 3 - Java
Another way of thinking of it is to expand the ++ expression:
++t1
is the same as putting t1 = t1 + 1
.
1) t1 = 5;
2) t2 = t1 + (++t1);
3) t2 = t1 + (t1 = t1 + 1),
4) t2 = 5 + (t1 = t1 + 1)
5) t2 = 5 + (t1 = 6)
6) t2 = 5 + 6 = 11
If you were to reverse the order to t2 = (++t1) + t1;
Then the expression would expand to:
1) t2 = (t1 = t1 + 1) + t1
2) t2 = (t1 = 5 + 1) + t1
3) t2 = (t1 = 6) + t1
4) t2 = 6 + 6 = 12
Solution 4 - Java
To add a point to Chris K,
The associativity is from left to right
So,
t2 = t1 + (++t1);
t2 = 5 + 6; // first t1 is replaced with 5 and then the next 6
t2 = 11;
Solution 5 - Java
The +
is evaluated from left to right, so
t1 + (++t1) // Left side is evaluated to 5, right side evaluated to 6...
5 + (6) // ...and as a side effect t1 becomes 6
Results in 11
.
Solution 6 - Java
The value of t1 in the second line is 5
t2 = t1 + (++t1)
t2 = 5 + 6; // t1 becomes 6 here
The evaluation order is from left to right.
So first t1 is evaluated to 5
then ++t1
to 6
and hence the result as 11
Solution 7 - Java
evaluation happen from left to right. So actually what happen is following
t2 = t1 + (++t1);
t2 = 5 + 6;
t2 = 11;
Solution 8 - Java
++x is executed before you use the variable x, and x++ increase x after you used it:
x=5;
y=x++;
y is 5;
and x is 6
x=5;
y=++x;
y is 6;
and x is 6
Solution 9 - Java
t2 = t1 + (++t1);
This is equivalent to
temp = t1 + 1
t2 = t1 + temp;
t1= temp;
Solution 10 - Java
enter code here
t1 = 5;
t2 = t1 + (++t1);
// it takes 5 for t1 and as it moves further to (++t1), it increments t1 to 6, so it takes 6 for (++t1)
t2 = 5 + 6;
// (++t1) this increments t1 by 1 then return new value. So (++t1)=6
// (t1++) this returns old value n then increments t1 by 1. So (t1++)=5