In Java, which is first executed, "+" or "++"?

I tried the following code in Java

t1 = 5; t2 = t1 + (++t1); System.out.println (t2); 

My opinion, since ++ has a higher priority than +, the above becomes

 t2 = t1 + (++t1); t2 = t1 + 6; // t1 becomes 6 here t2 = 6 + 6; t2 = 12; 

However, I get an answer of 11 for t2. Can someone explain?

+52
java operator-precedence
Sep 10 '14 at 9:02
source share
10 answers

You are almost right, but you misunderstand how priority 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); 

Result:

 11 12 

Priority does say to evaluate ++ to + , but this does not apply until it reaches this part of the expression.

Your expression has the form X + Y Where X is t1 and Y is (++t1)

First, the left branch is evaluated, i.e. X Then the right branch is evaluated, i.e. Y Only when it comes to evaluating Y is the ++ operation performed.

The priority rules say that ++ is "inside" the expression Y , they do not say anything about the order of operations.

+76
Sep 10 '14 at 9:12
source share

Your logic is close, but not entirely correct. Evaluation order from left to right for the + operator. t1 goes before the binary operator, LHS, and then the increment is on the RHS of this binary operand. LHS first.

 t2 = t1 + (++t1); t2 = 5 + 6; // t1 becomes 6 here as a side effect before being read on the RHS t2 = 11; 

Visualized as the tree you have

  + / \ t1 ++t1 

A priority

When two operators share an operand, the higher priority operator comes first. For example, 1 + 2 * 3 is considered as 1 + (2 * 3), while 1 * 2 + 3 is considered as (1 * 2) + 3, since multiplication has a higher priority than adding.

Associativity

When two operators are of the same priority, an expression is evaluated according to its associativity. For example, x = y = z = 17 is considered as x = (y = (z = 17)), leaving all three variables with a value of 17, since the = operator has left-handedness (and the assignment operator evaluates to the value on the right). On the other hand, 72/2/3 is considered as (72/2) / 3, since the operator / has left-right associativity.

+55
Sep 10 '14 at 9:05
source share

Another way of thinking is to expand the ++ expression:

++t1 is the same as setting 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 must reverse the order before t2 = (++t1) + t1; Then the expression will expand to:

 1) t2 = (t1 = t1 + 1) + t1 2) t2 = (t1 = 5 + 1) + t1 3) t2 = (t1 = 6) + t1 4) t2 = 6 + 6 = 12 
+15
Sep 10 '14 at 18:31
source share

To add a point to Chris K,

Associativity from left to right

So,

 t2 = t1 + (++t1); t2 = 5 + 6; // first t1 is replaced with 5 and then the next 6 t2 = 11; 
+5
Sep 10 '14 at 9:07
source share

+ is evaluated from left to right, therefore

 t1 + (++t1) // Left side is evaluated to 5, right side evaluated to 6... 5 + (6) // ...and as a side effect t1 becomes 6 

Results at 11 .

+3
Sep 10 '14 at 9:06
source share

The value of t1 in the second line is 5

 t2 = t1 + (++t1) t2 = 5 + 6; // t1 becomes 6 here 

evaluation order from left to right.

So, first t1 is evaluated to 5 , then ++t1 to 6 and, therefore, the result is 11

+2
Sep 10 '14 at 9:05
source share

assessment takes place from left to right. So what happens after

 t2 = t1 + (++t1); t2 = 5 + 6; t2 = 11; 
+2
Sep 10 '14 at 9:06
source share

++ x is executed before you use the variable x and x ++ increase x after using it:

 x=5; y=x++; y is 5; and x is 6 x=5; y=++x; y is 6; and x is 6 
0
Sep 10 '14 at 9:07
source share
 t2 = t1 + (++t1); 

It is equivalent

 temp = t1 + 1 t2 = t1 + temp; t1= temp; 
0
Sep 10 '14 at 9:10
source share
 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 
0
Sep 18 '14 at 10:09
source share



All Articles