• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confusion on postfix operator

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I WRITE THE FOLLOWING CODE
class Test
{
static int x = 12;
public static void main(String args[])
{
x = x++;
System.out.println(x);
x = ++x;
System.out.println(x);

}
}
THE OUTPUT IS 12 13
AND I THINK THAT OUTPUT SHOULD BE 13 14
WHY IS THIS SO.CAN ANYBODY HELP PLEASE
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by baber owais:
I WRITE THE FOLLOWING CODE
class Test
{
static int x = 12;
public static void main(String args[])
{
x = x++;
System.out.println(x);
x = ++x;
System.out.println(x);

}
}
THE OUTPUT IS 12 13
AND I THINK THAT OUTPUT SHOULD BE 13 14
WHY IS THIS SO.CAN ANYBODY HELP PLEASE


Hello,
Let's say if you have added an int y = 12, and then let x = y++, you would see the different answer as you expected. But when you assign x++ back to x (i.e x = x++), here is what happens:
First, since you tell x to postpone its increment, the RHS expression will save current x value (i. e. 12) in a temp / register. Next, it will increment x, x is now 13. Then the previous x value generated through RHS expression evaluation will be assigned to x via =operator, thus 12 is now written back for x. Therefore x does not change.
When you use the pre-increment, x takes on the value of 13, then the RHS expression is evaluated and then x finally is assigned via the =operator => thus it has a value of 13.
Hope that answers your question,
Lam
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lam,
Your explanation is correct, except for the part about assigning the post-incremented x to y. Try compiling/running this, you will see:
<pre>
public class testPost
{
public static void main( String[] args )
{
int x = 1 ;
int y = x++ ;

System.out.println( y ) ;
}
}</pre>

------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA

  • [This message has been edited by ryan burgdorfer (edited April 04, 2001).]
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ryan burgdorfer:
Lam,
Your explanation is correct, except for the part about assigning the post-incremented x to y. Try compiling/running this, you will see:
<pre>
public class testPost
{
public static void main( String[] args )
{
int x = 1 ;
int y = x++ ;

System.out.println( y ) ;
}
}</pre>


Hi Ryan,
Sorry!, I don't quite get what you want to say! I believe that I mentioned of the fact that if instead of letting x = x++, if one assign x = y++, y will increase its value by one.
In your example, you assign y = x++. That is a flip from what I said. And actually it does no matter what variables we use. The problem is this:
int x = 2;
x = x++;
(expectation x will be 3 - the answer is 2)
When I said about let x = y++, this is what I mean:
int y = 2;
int x = y++;
(expectation x is still 2 but y should be 3)
Similarly if we exchange x and y, we will get your code:
int x = 2;
int y = x++;
(expectation y will be 2 but x would be 3)
The last two cases fool people to into thinking that x = x++ would inccrement x by one. And they were suprised that it would not and retain its original! In short, people thought that post-increment would eventually force an increment to the variable that the ++operator apllied to. But that is not always as shown by x = x++, a very specific case where the LHS and the RHS deal with the same variable..post-increament operator's operation would be overriden!
Once again, I do not catch what you want to convey to me. If you can be specific, I would certainly try to entertain.
Regards,
Lam
 
reply
    Bookmark Topic Watch Topic
  • New Topic