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

Post- Increment for a variable

 
M.Sanjay
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Respeted Sir/Madam,
We have a doubt in the following code:-
class Sample
{
public static void main(String args[])
{
int i=10;
i=i++;
System.out.println("The value of i is: " + i);
}
}
When I run the program,it is giving the output as 10.
Why so?If I store the result in another variable,it is taking as 11.What is the difference between both the cases?
Thanking you,
M.Sanjay.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your name 'sanjaykumaresan' does not comply with the JavaRanch naming policy. Please spare a moment and re-register with a name that meets the requirements. We will take care of deleting your current login.
Thanks!
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the JavaRanch forum for discussing site-related issues only. I moving this discussion to Java in General(beginner)
forum.
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do a search in the forum. This question has been discussed earlier.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value held in the variable is the value BEFORE the post-increment. The value AFTER the post-increment is being help in "working memory" elsewhere.
 
Tim Anderson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this will do what you want:
i++;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic