• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Operators Precedence

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//......
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 ]
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 * / %
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 ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yah now it's clear and looks simple and silly
Thanks.
Sorry if i confused anybody.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic