Here's what n = n++ really does under the hood in Java:

1) tmp = n;
2) n++;
3) tmp = n;

The order of operations is really:
1) do all the pre-operations (like ++n)
2) evaluate the expression and assign to a temporary variable (i.e. push it
on the stack in Java)
3) do all the post-operations (like n++)
4) assign the temporary variable to the left hand side.

On Mon, Jun 7, 2010 at 6:26 AM, Eagle <[email protected]> wrote:

> Hi!
>      I find here C/C++ more consistent  and Java (umm..) "weird"!
> Because as I understand,  n = n++; is equivalent to the following two
> statements-
>      n = n;           // assignment first, effectively useless
>      n = n + 1;     //  incrementation afterwards
> while n = ++n; is equivalent to the following two statements -
>      n = n + 1;    // increment first
>      n = n;         // assign afterwards
> Both these should produce the same result (11 if n=10 before these)
> and this is what happens in C/C++.
> Java, however, produces different results. Surprising!
>
> Eagle
>
>
> On Jun 6, 8:29 am, Shoubhik <[email protected]> wrote:
> > Hello Reinier,
> >
> > Thanks a lot. I get your point.
> >
> >  " n holds value 10 [after int n = 10]
> > n holds value 11 [after n++]
> > n = 10 [after n = (result of n++)]  "
> >
> > this part of your explanation is extremely helpful. No doubt with the
> > Java experience anymore!
> >
> > But isn't the
> > precedence of unary operators Greater than "=" in C also ???
> >
> > So I was wondering why the result in C is so wierd :-)
> >
> > Hello Vinay,
> >
> > "In C++ and Java post increment operator first assigns the value and
> > then
> > increments"
> >
> >  the confusing part is that the "incrementation" did not take place :-(
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-codejam" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-code%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-code?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en.

Reply via email to