Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operator precedence

 
Minakshi Jain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • 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:
 
Yamuna Pattathil
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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.
 
Latha Kalaga
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic