This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Operators Precedence

nivas rao
Greenhorn
Posts: 9
//......
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 ]

H Gokulam
Ranch Hand
Posts: 46
Try this

Dimple Kaushik
Ranch Hand
Posts: 49
int a=1;
a += ++a + a++;
Evaluate from left to right.
a= 1+ ++a + a++;
a= 1+2+2 =5.

nivas rao
Greenhorn
Posts: 9
Thanks for the replies
Originally posted by dimple kaushik:
int a=1;
a += ++a + a++;
Evaluate from left to right.
a= 1+ ++a + a++;
a= 1+2+2 =5.

But OperatorPrecedence says Assignments are evaluated from right to left and i guess += is an assignment operator.
Can anybody clear my doubt?

prasanthi alla
Greenhorn
Posts: 15
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 .

prasanthi alla
Greenhorn
Posts: 15
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

Vicken Karaoghlanian
Ranch Hand
Posts: 522
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 '+='.

nivas rao
Greenhorn
Posts: 9
Thanks Vicken.
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 * / %
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 ]

Vicken Karaoghlanian
Ranch Hand
Posts: 522
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?

nivas rao
Greenhorn
Posts: 9
Yah now it's clear and looks simple and silly
Thanks.
Sorry if i confused anybody.

Anup Agrawal
Greenhorn
Posts: 19
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.
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