aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes postfix increment 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 "postfix increment" Watch "postfix increment" New topic
Author

postfix increment

pallavi utukuri
Ranch Hand

Joined: Feb 10, 2004
Posts: 182
given in khalid Rolf book
"i++ uses the current value of i as the value of the expression first, then adds 1 to i."
int i=0;
i=i++; //first the current value of i which is 0 is used as the value of the expression which makes i=0, then 1 is added to i which should make i=1
System.out.println(i);
but output is 0 it is should be 1 soo what did i miss here?


Thanks,<br />Pallavi
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Pallavi,
In this instance you should remember that the postfix increment takes place before the "=" assignment. So, it goes something like this:
  • JVM says "I'm going to set i to the current value of i which is 0"
  • Okay, now I can increment i by 1, to 1
  • Okay, now I can finally set i to 0 like I decided before.

  • A bit confusing. Luckily, doing this in real-life doesn't popup that often since it's obviously pointless.


    Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
    pallavi utukuri
    Ranch Hand

    Joined: Feb 10, 2004
    Posts: 182
    ys its confusing but better clear it rather hitting in the bush later......thanks a lot
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: postfix increment