aspose file tools*
The moose likes Beginning Java and the fly likes post increment operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "post increment operation" Watch "post increment operation" New topic
Author

post increment operation

Patrick Cicero
Greenhorn

Joined: Nov 06, 2002
Posts: 4
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
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

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 ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
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.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Vinod Chandana
Ranch Hand

Joined: Aug 26, 2003
Posts: 59
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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Aug 26, 2003
Posts: 59
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

Joined: Aug 26, 2003
Posts: 59
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: post increment operation
 
Similar Threads
I am not getting the required output...
Nested For Loops - understanding them
Post Increment
Array declaration
post increment confusion