Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

postfix increment

 
pallavi utukuri
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    pallavi utukuri
    Ranch Hand
    Posts: 182
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ys its confusing but better clear it rather hitting in the bush later......thanks a lot
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic