• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

operator precedence

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
Please explain this:
final public static void main(String args[]) {
int i = 0;
i = i++;
i = i++;
i = i++;
System.out.println(i); // prints 0, since = operator has the lowest precedence.
Minakshi
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The order in which things happen in statement execution is what causes this result.
1) - the left side asignment address to i is calculated
2) - the current value of i is grabbed
3) - the current value of i is incremented
finally, the value of i from step 2 is stored in the address calculated in step 1, replacing the value from step 3
Bill

------------------
author of:
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here the catch is that when u do something like i = i++ the incrementation will never take place.
ie, in your case i is always equal to zero.
Also try a search for this topic in this forum, u will find a lot of discussions.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying here that in
i = i++;
after assigning 0 to i, the incremented value is not stored back in i???
I guess I am confused by - the left side assignment address to i is calculated. Please could you explain??
Thanks.

Originally posted by William Brogden:
The order in which things happen in statement execution is what causes this result.
1) - the left side asignment address to i is calculated
2) - the current value of i is grabbed
3) - the current value of i is incremented
finally, the value of i from step 2 is stored in the address calculated in step 1, replacing the value from step 3
Bill


reply
    Bookmark Topic Watch Topic
  • New Topic