• 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

Marcus Green Question(can any body explain

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class a
{
public static void main(String str[])
{
int i=0;
i=i++;
System.out.println(i);
}
}
Ans is 0.
But in myview it should be 1. Can anybody Explains?
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i=i++
i) ++ is postfix operator. This line means assign (use) the
value to i and then (post increment) it.
Hence the existing value of i(=0) is assigned (and used ie printed)and then i is incremented.
Both of these are different.
i) i = i++;
ii) i++;
the second form prints the incremented value of i.
HTH
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rahul,
I presume you about the pre-increment and post-increment operators but you are questioning the logic behind it . Basically
<pre>
i = ++i;
can be translated as
i = i
i+1
i = i
where as
i = i++;
is translated as
i = i
i + 1
This can be better explained in assembler.
L R1,X load value in memory location X to register R1
L R2,X load value in mem loc X to reg R2
L R1,R2 load value in reg R2 to reg R1
A R2,=F'1' increment the value in register R2 by the const 1
ST R1,X store value in R1 back to memory location X
</pre>

The result of the final add which is in R2 is not stored back to memory hence it is discarded . Where as in i = ++i; the
value in register R2 is written back to memory location X.
I hope this helps.
Rgds
Sahir



[This message has been edited by Sahir Shah (edited November 21, 2000).]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think the key point of this question is which one has higher precedence. It is the unary operator " ++ ".




So in the statement i = i ++ , the expression i ++ will be evaluated first, all of us know the value of i ++ is 0, but after evaluation of i ++ , 1 has been stored back into i. Ok, the next evalution is assign the value of i ++ , which is 0 , to i . So what is the value of i after i = i ++ , it's 0...
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i=i++ can be broken into two simple statements:-
i=i;//i is 0 here
i=i+1;//i becomes 1.
When stored 0 is stored 1st and 1 after that. Now while printing with a stack approach Last in Last Out prints 0 and not 1.
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raj Mehra:
i=i++ can be broken into two simple statements:-
i=i;//i is 0 here
i=i+1;//i becomes 1.
When stored 0 is stored 1st and 1 after that. Now while printing with a stack approach Last in Last Out prints 0 and not 1.



What kind of processor are we talking about.
I thought stacks worked on a LIFO(last in first out) system.
Rgds
Sahir


[This message has been edited by Sahir Shah (edited March 27, 2001).]
 
Rahul Sharma
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
what about i=10
i=i+i++;
Output is 20
Pls explain
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As you saw earlier with i = i++; the initial value of i was used in the i = i assignment which was 0.
Hence
i = i + i++;
is the same as i = i + i;
ie. initial value 10 + 10 which gives you 20.
Rgds
Sahir
 
Raj Mehra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops!!! I meant it was First in Last Out. Thanks for pointing it out.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think sahirs original reply explains what happens most clearly.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rahul
I think you should forget the stack discussion for a start. It leads way off topic. And I don't thing the machine code posting answers the question either.
OK. The result of 0 is not not necessarily intuitive or obvious. To prove the point, note that the equivalent code in a C program prints 1! Here's a C program to try:
#include <stdio.h>
void main()
{
int i=0;
i=i++;
printf("%d\n", i);
}
(Our C compiler is locked at the moment, but my colleague assures me the C program prints out 1)
Now, as to the java. The behaviour obviously isn't a result forced by machine language or stack processing or whatever. It must reflect a design decision. Here's the best description of that decision I've found so far. It is from the Language Specification available at java.sun.com.


15.14.1 Postfix Increment Operator ++
PostIncrementExpression:
PostfixExpression ++
A postfix expression followed by a ++ operator is a postfix increment expression. The result of the postfix expression must be a variable of a numeric type, or a compile-time error occurs. The type of the postfix increment expression is the type of the variable. The result of the postfix increment expression is not a variable, but a value.
At run time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. .... The value of the postfix increment expression is the value of the variable before the new value is stored.


I interpret this to mean: treat i++ as an expression and evaluate that expression by adding 1 to i and (then) returnng the original value of i. So, in your original example, this means, set i to 1 then return 0.
Sahir's machine code is incompatible with this specification because the latter says "the sum is stored back into the variable" as part of the evaluation of the postfix increment expression. Sahir increments R2 but never stores it back in the location X. To be correct, he would have to store R2 back into location X, and the critical question is When? which Sahir doesn't answer. (I'm not having a go at you Sahir. I think you made a good contribution.)
So, now the full explanation of i = i++ (which is nonsense code anyway) is: set i to 1 and return 0; then, set i to 0.
The scary thing is that after I've been over this several times, it is starting to look intuitive to me and I'm wondering why C does it a different way.
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
greg,
It is true that the same instruction in C prints 1. I tested this out on microsoft's VC++. Logically, the value in Register R2 should be stored back to the memory location, and that is what the C compiler does. What I listed earlier was an approximation of what the java code does when the JVM converts the byte code to machine code instructions. I hope you arent serious about this making more sense to you than what the C compiler does.
Rgds
Sahir
[This message has been edited by Sahir Shah (edited November 23, 2000).]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raj Mehra:
i=i++ can be broken into two simple statements:-
i=i;//i is 0 here
i=i+1;//i becomes 1.
When stored 0 is stored 1st and 1 after that. Now while printing with a stack approach Last in Last Out prints 0 and not 1.


in stack we have first in last out - not last in last out.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic