wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg. post increment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg. post increment operator" Watch "Reg. post increment operator" New topic

Reg. post increment operator

Shruti Bakshi

Joined: Mar 06, 2005
Posts: 4
Can someone throw light on how the output of following code snippet is 0 ?

int i =0;
i = i++;

What happens to ++ ? Why is i not incremented after the assignment ?

SCJP 1.4
Guddu Jhakas

Joined: Feb 20, 2005
Posts: 9
here value of i is assigned first than it is post incrimented.
samjhe baaboo??

God is inside u<br />Belive him
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
as u know about the post increment operators,
when post increment operator is applied to a variable j:
first the current value of j is the value of the expression, and then later on the value of j increments.
suppose u have

So the final value is evaluated as:
1)result = j;
2)j = j + 1;
3) return result;

Now solving ur question this way, u get
result = i;
i = i + 1;
i = result;

Thats how it gets solved
Hope u r clear
also look at this

Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Just do a search on this forum and you will see that this has been beaten to death.
I agree. Here's the link: http://aspose.com/file-tools
subject: Reg. post increment operator
It's not a secret anymore!