First of all, I hope you understand that code like "i = i++" should never be used in a real world program, because it's confusing and most likely wrong. If you want to increment the value of i, you have to write just "i++" instead.
If you want to know why this:
int i = 2;
i = i++;
produces i = 2, look here: [url=http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment]Post increment operator and assignment[/url]
Why it is different from C / C++: Because Java is not C / C++ and the rules are just defined differently in Java.
> i=0;
> i=i++;
> should mean
> i is assigned the value of i then its 0 and then
> i should be incremented right
> so it should be 1 when i try to print
> why is it not happening ?
No
The expression i++ is evaluated, and the current value of i is saved for later.
i is incremented to 1
The saved value prior to the increment (0) is assigned back into i
The code is a complete waste of time. However, it teaches the value of understanding post-increment.
This is well defined in the JLS (Java Language Specification). The pertinent section can be found at http://java.sun.com/docs/books/jls/first_edition/html/15.doc.html#39438
...The value of the postfix increment expression is the value of the variable before the new value is stored.
What you are doing is no different than the following:
x = 0;
y = x++;
"The value of the postfix increment expression is the value of the variable before the new value is stored."
fine then according to this the value of the exp is the value of the
variable before tha value is stored
so the value of i is value of i before the increment
and anyhow after that the value is incremented and then new value is stored yes or no ?
Message was edited by:
bpcrao
what you need to understand is that the right hand side gets evaluated FIRST, before the result is applied to the left hand side.
so, the value of i does get incremented to 3, but then the i++ has a return value of 2, which gets assigned back to i.
you actually have two assignments occuring here. the first is an incrementation, and the second is assigning the return value of the incrementation, which is the same as the original value.
- Adam
> i tht thats the reason any proof as such from sun
> java ? or defined anywhere ?
paternostro quoted the official Java Language Specification above, which describes the official way this should work.
It does not work the way you think it should work. Why? Because the Java Language Specification says it works differently!
Continue complaining about it as you wish, but it's not going to make any difference! It works as it is supposed to work according to the official specification.
The specification says: i++ returns the old value of i (the value before it was incremented.
So if you do i = i++, the following steps are happening:
1. The value of i is incremented from 0 to 1.
2. The old value (0) is assigned to i.
The end result is that i is 0.
It is not difficult to understand.
> i know i ++ is post increment and ++i is pre
> increment
> why is it not working ?
It's working perfectly. It's behaving exactly as defined by the JLS.
i++ is an expression. It has a value and a side-effect. The value of the expression is the original value of i.
When you do <some_variable> = i++;
the variable will be assigned the value of i before it is incremented. This is true even if the variable is i itself.
i = i++;
is equivalent to
tmp = i; // save original value of i
i += 1; // add one to i (the ++ part)
i = tmp; // put the original value of i into the variable on the left side.
I'm sure this has all been explained elsewhere in this thread though.
> "The value of the postfix increment expression is the
> value of the variable before the new value is
> stored."
>
> fine then according to this the value of the exp is
> the value of the
> variable before tha value is stored
>
> so the value of i is value of i before the increment
> and anyhow after that the value is incremented and
> then new value is stored yes or no ?
>
Why would the new value be stored? "The value of the expression" is what's stored. That's what's meant by "the value of the expression". It means that, regardless of any side-effects, i++ itself has a value and if you stick in on the RHS of =, or as an arg to a method or c'tor, then that value--the expression's value--is what will be seen.
to put this topic to rest, take a look at an explanation from a True Java Expert, Joshua Bloch.
He covered this in his programming puzzlers talk at the 2005 Java One conference (TS-3738). If you view the media presentation, he will explain to you what the problem is. You can find it at http://developers.sun.com/learning/javaoneonline/2005/coreplatform/
Look at puzzle number 3 (Tricky Assignment).
int x = 0;
int i = 0;
x = i++;
it mean..
Ref = i;( Ref = 0 )
i = i+1;( i = 1 )
x = Ref;( x = 0 )
result x = 0 and i = 1
int i = 0;
i = i++;
it mean..
Ref = i;(Ref = 0 )
i = i+1;(i = 1 )
i = Ref;(i = 0 )
resutl i = 0;