Re: Q about increment and assignment
- From: "jupiter" <jupiter49byebyeSpam@xxxxxxx>
- Date: Sun, 24 Dec 2006 13:07:04 -0500
"John W. Kennedy" <jwkenne@xxxxxxxxxxxxx> wrote in message
news:c0ijh.1967$Bf.27@xxxxxxxxxxxxxxx
Big D wrote:
I'm confused by the output of the following code:
public class PP {
public static void main(String[] args) {
int i = 1;
i = i++;
System.out.println(i);
}
}
It outputs 1.
I understand the assignment operator happening before the ++,
but I don't understand why the ++ doesn't increment. I thought
the statement should basically expand to:
i = i;
i = i+1;
but the ++ gets lots somewhere...
No, it gets expanded into:
t = i;
John, is t a copy of the bits in i made by the postfix ++ method?
i = i + 1;
i = t;
Is it fair then to say that t gets restored because of the way the
postfix operator applies only after the expression is evaluated?
What is the difference between the original problem and this one?
int i=1;
System.out.println("i=" + i++); //output=1
System.out.println("i=" + i);//output=2
I find this one easier to understand because eventually the new
bits do output. They don't get overwritten. In the original problem
there is overwriting going on that cause the 2 value to be lost
entirely. Is this due to the self-referencing aspect of the
original problem i=i++;?
One last question: Where is that temp copy of the bits visible? I
can't see it in a debugger, so maybe I'm not using it (Eclipse) to
great advantage. I see the original i bits on the stack and that's
about it. This should not involve heap storage, so that leaves me
wondering where/how I'd ever see that temp set of bits.
thanks ...
.
- Follow-Ups:
- Re: Q about increment and assignment
- From: Patricia Shanahan
- Re: Q about increment and assignment
- From: John W. Kennedy
- Re: Q about increment and assignment
- References:
- Q about increment and assignment
- From: Big D
- Re: Q about increment and assignment
- From: John W. Kennedy
- Q about increment and assignment
- Prev by Date: Re: Split group?
- Next by Date: Re: YTUTTTY
- Previous by thread: Re: Q about increment and assignment
- Next by thread: Re: Q about increment and assignment
- Index(es):
Relevant Pages
|