aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green Question(can any body explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green Question(can any body explain" Watch "Marcus Green Question(can any body explain" New topic
Author

Marcus Green Question(can any body explain

Rahul Sharma
Greenhorn

Joined: Oct 03, 2000
Posts: 6
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?
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
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
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158
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).]


....
Edy
Greenhorn

Joined: Nov 16, 2000
Posts: 4

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...
Raj Mehra
Ranch Hand

Joined: Nov 20, 2000
Posts: 51
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.


SCJP2 2001 84%<br />SCJD2 2003 100%
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158
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

Joined: Oct 03, 2000
Posts: 6
Hi
what about i=10
i=i+i++;
Output is 20
Pls explain
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158

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

Joined: Nov 20, 2000
Posts: 51
Oops!!! I meant it was First in Last Out. Thanks for pointing it out.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i think sahirs original reply explains what happens most clearly.


SCJP
Visit my download page
greg clarke
Greenhorn

Joined: Oct 06, 2000
Posts: 25
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

Joined: Nov 05, 2000
Posts: 158
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).]
chandresh upadhyay
Greenhorn

Joined: Nov 23, 2000
Posts: 3
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green Question(can any body explain