• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operators Precedence

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following peice of code :
void meth()
{
int a=5,b=5;
System.out.println(a++ + ++b + (b=a)) // o/p is 17 ???
}
Plz outline me the precedence of operators here
Thanx
Kaushik
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kaushik,
If increment operator is placed BEFORE the variable, this variable will be incremented first, before doing any other operation. If increment operator is placed AFTER the variable, this variable will be processed with the next operande and then incremented. Thus, in this case:
(a++ + ++b + (b=a))
b is incremented first(6);
a is added to b(5+6=11);
a is incremented(6);
value of a is assigned to b(b now equals 6);
6 is added to 11(17)
HTH,
Igor
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here
a++ is 5 for purpose of addidtion, but then it has increased by one(to 6) as a++ is post increment,
++b = 6, as ++b is pre increment
then it add b which is assigned to be equal to a , which is equal to 6 (after increrment of one), that why 5 + 6 + 6 =17
HTH
--Farooq
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
((a++) + (++b)) + (b=a)
i think this is how the equation can be interpreted


hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
 
Kaushik Badiyani
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Samith Nambiar:
((a++) + (++b)) + (b=a)
i think this is how the equation can be interpreted

hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo


My main doubt over here is wont(b=a) be executed first
Because Inner brackets r executed first..
Plz help me on this doubt
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Koushik,
If u see the oprator precedence list u will find that ++ and () has the same (highest) precedence and so if both of them appear ina single expression then the order of evaluation will be from left to right. So if u change the order of appearance of () like this code u will get O/P 16 instead of 17.

Hope it helps u.

------------------
azaman
reply
    Bookmark Topic Watch Topic
  • New Topic