aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operator precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operator precedence" Watch "operator precedence" New topic
Author

operator precedence

Minakshi Jain
Greenhorn

Joined: Dec 11, 2000
Posts: 4
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

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Dec 03, 2000
Posts: 19
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

Joined: Nov 13, 2000
Posts: 96
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operator precedence