wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Post- Increment for a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Post- Increment for a variable" Watch "Post- Increment for a variable" New topic
Author

Post- Increment for a variable

M.Sanjay
Greenhorn

Joined: Jun 29, 2001
Posts: 5
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

Joined: Mar 17, 2000
Posts: 5782
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!


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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

Joined: Feb 06, 2001
Posts: 358
Do a search in the forum. This question has been discussed earlier.


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Tim Anderson
Greenhorn

Joined: Jun 29, 2001
Posts: 6
this will do what you want:
i++;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Post- Increment for a variable