The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000

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:

little difference!!

Posted by swapnil on November 27, 2000 at 7:23 AM

d/kannapan,
i shall try to expalin the difference to u.

case 1.
x=0;
x=x++;

In the above program , the first line is assigning zero to x.
In the second line, there are two operators viz assignment operator and the increment operator.When we write x=x++ then the assignment operator gets the preference and the value of R.H.S. x(i.e. zero) is assigned to the L.H.S.(left hand side) x. The increment operator then is operational .so the increment operation is executed. But the incremented value is NOT assigned to any variable.So the incremented value is lost.
now if we print the value of x , it prints the original value i.e. zero.


case 2.
x=0;
x=++x;

In this case the initial value of the x variable is zero.
In the second line we see there are two operations viz assignment and the increment.Here also the assignment operator gets the preference.But the value that is assigned to L.H.S x is the value after the increment of the R.H.S. x because the increment operator is preceding the R.H.S. x . This incremented value is being assigned to L.H.S. x . So now the value of x is 1 i.e. one.

hope this helps.
regards,
swapnil





Replies:

Sponsored Links



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