• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bowled over by a simple code : Part-2

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What should be the output of this simple piece of code:
public class test{
public static void main(String[] arg){
int i = 0;
i = i++;
System.out.println("i1 = " + i);
i = i++;
System.out.println("i2 = " + i);
i = i++;
System.out.println("i3 = " + i);
}
}
The answer is,
i1 = 0
i2 = 0
i3 = 0
Somehow the increments never happen and I so far haven't got a decent explaination...
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay Narayanan:
The variable i is indeed incremented
Ex:
class Ill{
int i = 0;
i = i ++;
System.out.println(i);
}
//print 0
To know if i is incremented or not use utility javap -c from jdk.
javap -c Ill :
You'll get this:

Hope this help
For additional explanation why this code prints 0 0 0 check this thread
[This message has been edited by lu v thuan (edited October 31, 2001).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijay Narayanan:
What should be the output of this simple piece of code:
public class test{
public static void main(String[] arg){
int i = 0;
i = i++; **1**
System.out.println("i1 = " + i);
i = i++;
System.out.println("i2 = " + i);
i = i++;
System.out.println("i3 = " + i);
}
}
The answer is,
i1 = 0
i2 = 0
i3 = 0
Somehow the increments never happen and I so far haven't got a decent explaination...


Vijay,
There is nothing complex going on here. The mechanics of the post increment operator is such that the value is first assigned and then incremented.
In your case, at **1**, i is first assigned 0 and then incremented. The incremented value 1 is stored in a temporary register. Since the outcome of i++ is assigned to i, the value in the temporary register is rendered ineffective. As a result, irrespective of the number of "i = i++" statements you write, the result will always be the same.
Try just i++ or use the pre-increment operator, the results will be different. (In this case, Both will yield the same results)

Hope this helps
Shyam
[This message has been edited by Shyamsundar Gururaj (edited October 31, 2001).]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay and Shyam
As I understand it, it is the original value of the variable that is stored in the temporary register so that the process is like this:
From the JLS Section 15.14.1

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


For the simplified case of x = x++;
What happens is that the initial value of x is stored in a temporary register, then x is incremented, then the value stored in the register is asigned to the left hand side of the expression, in this case the LHS is x so x gets its original value.
int x = 1;
x = x++;
Steps:
1 initial value of x is stored in temp register. So temp = 1.
2 x is incremented. x = 2 and temp = 1.
3 the value of the temp register is assigned to the LHS. x = 1
If you think about it this makes sense to store the original value and then use it in the expression evaluation, that way when your done evaluating the expression you just substitute in the value in the temp register for the original variable. If you store the incremented value then the compiler would have to check to see if your assignng the result of the expression to the original variable and invalidate the value in the temp register - this seems like a lot of work to make something this confusing happen. By doing it the first way, the subject of this entire discussion just happens as a by product of the way the post-increment operator works, this it gives us a cahnce to have these interesting conversations and amaze and astound our friends.
hope that clears it up


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Dave! you're right...Thanks a lot
Shyam
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic