• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

post increment operation

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was looking at some certified Java programmer sites, when I came across the
following code:
public static void main(String[] arguments)
{
int i = 0;
int j = 5;
i = i++;
System.out.println("value of j is = "+j);
System.out.println("value of i is = "+i);
}
When run the result is:
value of j is = 5
value of i is = 0
My question is I assume that the i would be set to 0 by the
assignment of i = i. But then before running the next statement
i would be incremented to a value of 1. Based upon the running of the
code this is not what happens.
Can someone please explain why i is not incremented after being assigned
the value of 0.

THANKS
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has something to do with how the underlying VM work.
All your variable is stored in a specific memory location,
and the auto-increment(++) or auto-decrement(--) operate directly on
that location. While regular arithmatic operation and assignment
is done on separate memory location and THEN the result is copy back into
your variable memory location.
So when you do

What happen is:
1. VM found that i is assignment operation, but found that it has post-increment. So it copy the value of original i to temporary scratch paper.
2. the increment happen to the original i location
3. since there is no other operation happen, it copy the result in the scratch paper(which is still the original value) back and overwrite the incremented value. So basically nothing change.
If you just want to increment i, you don't have to use the assignment.
Just use [CODE]i++;[CODE] without assignment.
Hope that make sense.
[ October 01, 2003: Message edited by: Wirianto Djunaidi ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, a couple of lessons to extract from this might be:
1. Assignments happen last.
2. Don't write code with compound statements like i = i++; . Write code that is clear and easy to understand.
[ October 08, 2003: Message edited by: Dirk Schreckmann ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I think this is a result of a misinterpretation of the post-increment operators definition.
The post-increment (in spite of its name) does *not* defer the incrementation. It increments the variable *immediately*, but *the value of the expression* is that of the variable *before* the increment.
Did that help?
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let me add my $0.02, I think the code is a tricky one.
when you do i = i++, what the JVM thinks is this:
It does the assignment first, so the statement of i = i++; is essential this: i = i;.
Then the JVM does an increment, however, since the assignment already performed, it just won't do anything to i.
If you do ++i instead of i++, or if you just do i++ instead i = i++ then the output should be 1 instead of 0.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at i++ as an expression. It returns i and as a side effect increments i. So in this case it returned 0 and set i=1. Then the assignement happened using the result of the expression which was 0.
I go with the advice, never combine these operators with anything else if you can help it. i++ is a perfectly nice line of code but anything longer gets scary.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really weird. I've tried a similar thing in C and C++, the value of i is one in both the cases. Try this thing too. i = (i++); Even this is returning 0!!!
- Vinod

Originally posted by Stan James:
Look at i++ as an expression. It returns i and as a side effect increments i. So in this case it returned 0 and set i=1. Then the assignement happened using the result of the expression which was 0.
I go with the advice, never combine these operators with anything else if you can help it. i++ is a perfectly nice line of code but anything longer gets scary.

 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vinod Chandana:
This is really weird. I've tried a similar thing in C and C++, the value of i is one in both the cases. Try this thing too. i = (i++); Even this is returning 0!!!


This will depend on the C/C++ compiler you use. With other words: the result of this statement isn't defined in C++! (In Java, it is.)
 
Vinod Chandana
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:

This will depend on the C/C++ compiler you use. With other words: the result of this statement isn't defined in C++! (In Java, it is.)


That could be true. But I've tried a couple of compilers, it works fine. Think about i = (i++); It is really weird.
- Vinod.
 
Vinod Chandana
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vinod Chandana:

That could be true. But I've tried a couple of compilers, it works fine. Think about i = (i++); It is really weird.
- Vinod.


Ignore my query abt i = (i++). Thanks, Vinod.
 
Bras cause cancer. And tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic