?
Solved

i = i++

Posted on 2003-02-27
6
Medium Priority
?
211 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:
Suggested Courses

621 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