?
Solved

i = i++

Posted on 2003-02-27
6
Medium Priority
?
198 Views
Last Modified: 2010-03-31
Why does this print 5 instead of 6?
          int i = 5;
          i = i++;
          System.out.println(i);
0
Comment
Question by:VinoTinto
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8035337
 Because it is post increment. It increments the variable i after it assigns the value to it. To print 6 you have to do:

  i = ++i;
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8035829
The reasoning of this (I believe) is that primitives in Java are immutable.

so when you do i=i++;
it creates a new memory location for 'i' and points it to the original value (in this case 5)
then it takes the original memory location of i and increments it (in this case to 6) but since 'i' now points to a new memory location (which is pointing to 5) you will get 5 printed out.

On the other hand if you do:
i++; // instead of i=i++;
you are incrementing the original memory location.

I believe this is what happens.  Please correct me if I am wrong.

CJ
0
 
LVL 35

Accepted Solution

by:
girionis earned 400 total points
ID: 8036422
 There are certainly two different memory locations involved. A very interesting discussion can be found here:

http://forum.java.sun.com/thread.jsp?thread=1505&forum=1&message=3232

  and the the Sun engineer suggests:

"this is most definitely NOT a bug, and this is actually what
happens...
int i=0;
i=i++;
1) "i" is initialised to 0 and it is pushed onto the stack in
JVM.
2) the value on the top of the stack is popped and is then copied
into a local variable (index to "i") known to the machine.
3) the value of the local variable is pushed onto the stack.
4) the local variable is incremented by 1.
5) the value on the top of the stack (still 0) is popped and is
then copied into the local variable (index to "i")
6) the assignment took place and "i" is assigned the value 0.
this will be repeated for a further 2 times.
the key to understand what is happending is that the JVM uses 2
different locations for storing the values of "i" temporarily:
one on the stack and one in the local variable.
if it is a prefix operation eg ++i; the order would be:
1) "i" is initialised to 0 and it is pushed onto the stack in
JVM.
2) the value on the top of the stack is popped and is then copied
into a local variable (index to "i") known to the machine.
3) the local variable is incremented by 1.
4) the value of the local variable is pushed onto the stack.
5) the value on the top of the stack (now 1) is popped and is
then copied into the local variable (index to "i")
6) the assignment took place and "i" is assigned the value 1.
you can examine the bytecode generated by the compiler and study
the sequence of instructions by issuing the command
javap -c yourfile
yourfile must be the .class file
you can find the specifications for the JVM at
http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions.doc.html
hope this helps"

  I beleive there are some realyl interesting things going on behind the scenes of the JVM. I could never have thought that this expression:

int i = 0;
i = i++ + i++ + i++ + i++ + i++ + i++;
System.out.println(i);

  would actually evaluate to 15 while this:

int i = 0;
i = i++ + i++ + i++ + i++ + i++ + i++ + i++;
System.out.println(i);

  would actually evaluate to 21.

  Furthermore this one:

int i = 0;
i = i++ + i++ + i++ + i++ + i++ + i++ + i++ + i++;
System.out.println(i);

  would evaluate to 28!

  There is a pattern going on here. The expression is equal to the previous result plus the number of previous "i++" incrementations added.

  So with 1 "i++" the result is 0
  With 2 "i++" added the result is (0 + 1) = 1
  With 3 "i++" added the result is (1 + 2) = 3
  With 4 "i++" added the result is (3 + 3) = 6
  With 5 "i++" added the result is (6 + 4) = 10
  and so on!!

  Interesting.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Expert Comment

by:girionis
ID: 8036460
 I am just thinking that i would be very interesting (and also very evil) to use this pattern within loops in commercial code. We guaranteed to foil the maintenance programmer. :D
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8036797
:-)

I was told this is pretty much borrowed directly from C

CJ
0
 

Author Comment

by:VinoTinto
ID: 8037046
Thanks to both of you. Very good and detailled answer. Exactly what I was looking for.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question