//...... int a=1; a += ++a + a++; System.out.print(a);// prints a= 5 //... But According to Operators Precedence the order of operators in this code is post increment, pre increment, assignment a+= ++a + 1// now a=2 a+= 3+1 a= 3+4 Then acc to me a should be 7 Did i misunderstand the concepts??
[ December 01, 2003: Message edited by: nivas rao ] [ December 02, 2003: Message edited by: nivas rao ]
hi nivas! += is an assignment operator ,but still the rule of evaluation of assignment operators from right to left is followed when there r more than one of such operator is present in the expression. but now iam also confussed of post operator and preoperator precedence by seeing the manual in the link provided in ur question. otherwise as its an unary operator it had equal precedence and so evaluated from left to right .
Joined: Dec 02, 2003
hi iam very new to this site ,so pardone me if i have not given proper reply, trying to teach myself from this site bye prasanthi
Welcome to the ranch nivas, yes you are correct the assignment operator is evaluated from right to left. However the post and pre-increment operators have a higher priority so they are executed first, then followed by the '+' operator and finally by the '+='.
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Joined: Dec 01, 2003
Originally posted by Vicken Karaoghlanian: Welcome to the ranch nivas, yes you are correct the assignment operator is evaluated from right to left. However the post and pre-increment operators have a higher priority so they are executed first, then followed by the '+' operator and finally by the '+='.
Yes i agree post and pre increments hv higher priority than assignment and even post has higher priority than pre. And this is exactly what i applied earlier and i got 7 in the above example which is wrong. As for java.sun.com OperatorsPrecedence ------------------- postfix operators  . (params) expr++ expr-- unary operators ++expr --expr +expr -expr ~ ! creation or cast new (type)expr multiplicative * / % additive + - shift << >> >>> relational < > <= >= instanceof equality == != bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | logical AND && logical OR || conditional ? : assignment = += -= *= /= %= &= ^= |= <<= >>= >>>= When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated in left-to-right order. Assignment operators are evaluated right to left. [ December 02, 2003: Message edited by: nivas rao ]
Joined: Jul 21, 2003
Nivas, let me simplify the equation a little bit. a += ++a + a++ is the same as a = a + ++a + a++, right? Good. Now the assignment operator '=' is evaluated at last. The RHS of the equation is evaluated from L --> R as follows a = 1 + 2 + 2 thus giving us 5. is it clear now?
Joined: Dec 01, 2003
Yah now it's clear and looks simple and silly Thanks. Sorry if i confused anybody.
Hi Nivas, Please read carefully the posts by Dan. It is in the end of the thread as suggested by Hari. The important thing Dan Said The reason for the difference is the fact that the JVM processes the expression from left to right. The JVM can not look at the entire expression as we can. While a human can start the evaluation of an expression from the inner most parenthesis at any point in the expression--left, right, or center--the JVM must start parsing the expression on the left and work toward the right. therefor in your example int a = 1 a += ++a + a++ I think what JVM might do is a = a + ++a + a++ JVM just start replacing the values a = 1 + then it finds a uanry operator it changes its value to 2 and then another additive operator and 2 then another additive operator a = 1 + 2 + 2 = 5 Lets try another example
Let us evaluate as we human being will do to to the above code we will first evaluate ++z in the end z += z++ + x + y/4 ////now z is 4
z += 4 + x + y/4 //// now z is 5 z += 4 + 17 + 60/4 z = 5 + 36 z = 41 which is INCORRECT. It is because we human can see the expressions at one time but it is not the case with the compilers they have to parse in a a order that is from either left to right or right to left. lets see how the JVM evaluates z = z + z++ + x + y / ++z /////line 1 z = 3 + 3 + 17 + 60 / 5 /////line 2 z = 35 In line 1 JVM first pareses it left to right If you notice after line 2 it still follows the operator precedence first it evaluated the multiplicatibe operator (/) and then the additve operator. I hope it will help