This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tricky assignment i=i++;

 
Stefan Koeltze
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All!
int i =0;
i = i++;
System.out.println(i);
this prints out 0 and not the expected 1!
First i is assigned by i which is 0. Then i should be incremented to 1 !???
Thanks a lot!
Ciao Stefan
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ++ operator is called the post increment operator. It increments after the assignment occurs. There is also a pre increment operator. The code for that is ++i. That will increment i before it is assigned.
 
chafule razgul
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
++ is a "postfix increment operator"
what that means in this case is that the original value of i (that is, 0) is assigned to i, then i is incremented.
you might want to check out JLS 15.14.1 for additional infromation
Hope this helps
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope,
you are all wrong guys...
++ is performed before the assignment but the assignment uses the value of i before the incrementation takes place.
If the incrementation took place after the assignment then i would become 1.
you should do a search in this forum for post-increment operators, but basically here is how it works:
when you do i++, the value of i (i.e. 0) is stored and it is that one that will be used in the assignment later, then i is incremented to 1. Then the assignment takes place and 0 is assigned to i again, thus i will always be 0.
[ February 27, 2002: Message edited by: Valentin Crettaz ]
 
Stefan Koeltze
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, but
I did the same in C++
and there the output is ...
1
Ciao Stefano
 
Lionel Siau
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If u thought that was bizzare, try this one
int i=0;
i = i++;
i = i++;
i = i++;
System.out.println(i);
Guess what i is!!!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely 0 !!! Which is perfectly normal
[ February 27, 2002: Message edited by: Valentin Crettaz ]
 
Brian Lugo
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a link to the Post-Fix operator post:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014701
I hope this will help you understand better the post-fix operator ...
Brian
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy I don't think the original question had anything to do with the post-fix increment.
Correct me if I am wrong, but the code highlights an odd behaviour in my view of things
in the statement i = i++;
the value i is assignment to itself, then i itself is increment (we are talking about the same variable!!)
so it seems like i is assigned a value of zero, but then never incremented?
I would have guessed that i = i++; would break down into the steps
i = i;
++i;
which would imply that the output should be '1' not '0'
I think they got it right with C++
[ February 27, 2002: Message edited by: Rajinder Yadav ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again, this has to do with how the ++ is performed in Java.
From JLS 15.14.1 Postfix Increment Operator ++

The value of the postfix increment expression is the value of the variable before the new value is stored.

Conforming to what Rajinder said, this is firstly a operator precedence problem, that is, i++ is evaluated before the assignment, BUT i++ is an expression in itself and the most important thing here is that i++ evaluates to the value of i BEFORE the incrementation occurs.
So when evaluating i++, the current value of i (0) is stored somewhere (not relevant here), then the incrmetation proceeds (at which time i is 1) and then the assignment finally occurs, which has the effect of assigning the stored value (0) to i again. That's why i will always be 0.
Moreover, what is the advantage of writing i=i++ since by doing i++ i's value will be incremented anyway?
[ February 27, 2002: Message edited by: Valentin Crettaz ]
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Valentin
Originally posted by Valentin Crettaz:
Moreover, what is the advantage of writing i=i++ when only by doing i++ i's value would be incremented?[/QB]
 
chafule razgul
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the most important thing here is that i++ evaluates to the value of i BEFORE the incrementation occurs.
So when evaluating i++, the current value of i (0) is stored somewhere (not relevant here), then the incrmetation proceeds (at which time i is 1) and then the assignment finally occurs, which has the effect of assigning the stored value (0) to i again. That's why i will always be 0.

In this case, how would one describe ++i? the incrementation proceeds B]before[/B]storing the current value?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS 15.15.1 Prefix Increment Operator ++

The value of the prefix increment expression is the value of the variable after the new value is stored.

[ February 27, 2002: Message edited by: Valentin Crettaz ]
 
chafule razgul
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did read the JLS prior to the post, i just didn't understand it..
Thank you Valentin.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize chafule...
Bad day I guess :roll:
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Valentin, is i++ and ++i an atomic operation? Ie, thread-safe?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you use double I'd say that it is atomic...
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, here is what the JLS say about that.
From JLS 2ed (17.4 Nonatomic Treatment of double and long)
If a double or long variable is not declared volatile, then for the purposes of load, store, read, and write actions they are treated as if they were two variables of 32 bits each: wherever the rules require one of these actions, two such actions are performed, one for each 32-bit half.
Originally posted by Rob Ross:
Hey Valentin, is i++ and ++i an atomic operation? Ie, thread-safe?
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about i++ and ++i for non-doubles or non-longs?
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob to sum it up, 32-bit operations are atomic.
If we talk about micro-code, it's true that an increment operation on a 32-bit value will take 3 steps
load value into register
add 1
copy value from register
but let's stick to 32-bit opcode as java does
[ February 27, 2002: Message edited by: Rajinder Yadav ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i++ when i is an int is translated into the following JVM instructions:
1. iload_1
2. iinc 1 1
On line 1, i's value (the first variable in the local variable pool) is loaded onto the stack.
On line 2, the top-most operand (i's value) is incremented by one.
When i is a double, the instructions look like:
1. dload_1
2. dconst_1
3. dadd
On line 1, i's value (the first variable in the local variable pool) is loaded onto the stack.
On line 2, the constant 1.0 is loaded onto the stack.
On line 3, the previous two operands are added together.
So, actually, an int is incremented in one operation (regardless of the value's loading on the stack) while it takes 2 operations with doubles and longs.
[ February 27, 2002: Message edited by: Valentin Crettaz ]
 
Stefan Koeltze
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi barkeepers
Finally I want to say:
i=i++; is part of an exam question (www.jchq.net). I also believe that it makes not that sense.
Thanks a lot 4 your interest!
Ciao Stefan
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan,
I've also included i=i++ in my mock and I wouldn't say that it doesn't make any sense. In the contrary, it makes a lot of sense, since it emphasizes the importance of how postfix operators work. If you don't pay enough attention to such kind of tricky things you may end up with very weird bugs... So in my opinion, a question including i=i++ makes a loooot of sense since it shows you what NOT to do.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic