File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tricky assignment  i=i++; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Tricky assignment  i=i++;" Watch "Tricky assignment  i=i++;" New topic
Author

Tricky assignment i=i++;

Stefan Koeltze
Greenhorn

Joined: Feb 08, 2002
Posts: 14
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

Joined: Mar 09, 2001
Posts: 2676
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.


Matthew Phillips
chafule razgul
Ranch Hand

Joined: Feb 09, 2002
Posts: 63
++ 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

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Stefan Koeltze
Greenhorn

Joined: Feb 08, 2002
Posts: 14
Thanks a lot, but
I did the same in C++
and there the output is ...
1
Ciao Stefano
Lionel Siau
Greenhorn

Joined: Feb 27, 2002
Posts: 12
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

Joined: Aug 26, 2001
Posts: 7610
Definitely 0 !!! Which is perfectly normal
[ February 27, 2002: Message edited by: Valentin Crettaz ]
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
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

Joined: Jan 18, 2002
Posts: 178
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 ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Jan 18, 2002
Posts: 178
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

Joined: Feb 09, 2002
Posts: 63
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Feb 09, 2002
Posts: 63
I did read the JLS prior to the post, i just didn't understand it..
Thank you Valentin.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I apologize chafule...
Bad day I guess :roll:
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Hey Valentin, is i++ and ++i an atomic operation? Ie, thread-safe?


Rob
SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Unless you use double I'd say that it is atomic...
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
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

Joined: Jan 07, 2002
Posts: 2205
what about i++ and ++i for non-doubles or non-longs?
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Feb 08, 2002
Posts: 14
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

Joined: Aug 26, 2001
Posts: 7610
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tricky assignment i=i++;
 
Similar Threads
Bit twiddling
Apply 2d array of answer to the right position of JTextField grid with JCheckBox
Int array to 3D array
how to combine between two arraylist if has same value
Question on multi dimensional array