Re: Q about increment and assignment




"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 ...






.



Relevant Pages

  • Re: Q about increment and assignment
    ... public static void main{ ... I understand the assignment operator happening before the ++, but I don't understand why the ++ doesn't increment. ... In the original problem there is overwriting going on that cause the 2 value to be lost entirely. ... It isn't visible at the Java level, though it will be seen in the bytecode (does Eclipse have an option to trace at the bytecode level? ...
    (comp.lang.java.programmer)
  • Re: Q about increment and assignment
    ... int i = 1; ... I understand the assignment operator happening before the ++, but I don't understand why the ++ doesn't increment. ... In the original problem there is overwriting going on that cause the 2 value to be lost entirely. ...
    (comp.lang.java.programmer)
  • Re: Q about increment and assignment
    ... public static void main(Stringargs) { ... int i = 1; ... I understand the assignment operator happening before the ++, but I don't understand why the ++ doesn't increment. ...
    (comp.lang.java.programmer)
  • Re: Q about increment and assignment
    ... public static void main(Stringargs) { ... int i = 1; ... I understand the assignment operator happening before the ++, but I don't understand why the ++ doesn't increment. ...
    (comp.lang.java.programmer)
  • Re: Q about increment and assignment
    ... public static void main(Stringargs) { ... int i = 1; ... I understand the assignment operator happening before the ++, ...
    (comp.lang.java.programmer)