The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

++ puzzle

Posted by Kishori Sharan on June 08, 2001 at 3:31 PM

int i = 1 ;
i = i ++ ;

1) ( int i = 1 ) : i is initialized to 1
2) ( i = i ++ ) : i++ is evaluated. This makes the expression as
i = 1 because i++ uses post increment operator.
3) Now i is incremented to 2 , that is the effect of post increment operator ++.
4) At this stage the expression i = i++ is in form i = 1 and in memory i has been incremented to 2.
5) Now i = 1 is executed which makes i equla to 1.\
6) Finally i is 1 after i = i++ is executed.

The important thing to remember is that in i++ , first the value of i is substituted in place of i++ and then i is incremented immediately . Java dosn't wait to inclrement the value of i util i = i++ whole expression is executed.

Thanks
Kishori






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us