• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Post increment operator

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code that I tried:

int i = 10;
System.out.println(i);
i = i++;
System.out.println("i=" + i);

and i got the output as i=10

why the incremented value of i is not stored.

But if i change the stmt i=i++ to j=i++
i is incremented and the incremented value is stored in i
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if i change the stmt i=i++ to j=i++
i is incremented and the incremented value is stored in i


That is a good clue as to what is happening.

In the first case:
1. i (10) is copied onto the stack from its original location
2. the original i is incremented to 11
3. the copy of i (10) on the stack is copied into the original i
i is 10

In the second case:
1. i (10) is copied onto the stack from its original location
2. the original i is incremented to 11
3. the copy of i (10) on the stack is copied into j
i is 11
j is 10

now try this version:
i = ++i;
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rats, Mike beat me to this. I was going to quote him from another post.

I put in a link to the post because some people seem to ask more questions even after the elegant answer.
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic