aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Basics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Basics" Watch "Basics" New topic
Author

Basics

luis meira
Ranch Hand

Joined: Apr 18, 2002
Posts: 55
Hello everyone,
i'm having a hard time understanding this code :
int c = 0;
c += c++;
System.out.print ( c );
result is equal to 0 and not 2 has i was thinking.
can anybody explain me why ?
best regards
Lu�s Meira
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
luis
c += c++ is equivalent to:
c = c + c++
Before the operation is carrried out all of the operands are fully evaluated, c is equal to 0, and the result of the postfix increment is the original value of the operand so the value of c++ is also 0.
The reason it prints 0 then instead of 1 (or 2 as you thought) is becasue even the though the c++ increment sthe value by 1 that value is immediately replaced by the 0 that is assigned to c.
hoep that helps, if you are having confusion on the postfix operator there are plenty of threads on it in this forum, just do a search for it.


Dave
brent spearios
Ranch Hand

Joined: Apr 10, 2002
Posts: 48
int c = 0;
c += c++;
System.out.print ( c );

c=0 therefore c+=c++; is the same as saying c=0+0;
because the ++ is done after the evaluation, if you did c+=++c; you would get 1.
luis meira
Ranch Hand

Joined: Apr 18, 2002
Posts: 55
Originally posted by brent spearios:
int c = 0;
c += c++;
System.out.print ( c );

c=0 therefore c+=c++; is the same as saying c=0+0;
because the ++ is done after the evaluation, if you did c+=++c; you would get 1.

that's what i don't understand if ++ is avaluated
last by the time i do System.out.print ( c );
c should be 1.
i was thinking something like :
c = 0 + 0;
c++;
System.out.print ( c );
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
It's important to remember what's in the variable c while each piece of the expression is being evaluated. Let's go through it, step by step.
The code as is:

The first thing the JVM is going to do is evaluate each argument in the expression. I'll do this piece by piece and show what c is at each step. Let's start with the fact that c += c++ is equivalent to c = c + c++.

As you can see, in the line labelled "Look here" the expression c++ is evaluated to 0 and then c is incremented to 1. Now, however, if you continue with the evaluation of the expression, the right hand side evaluates to 0. This value is then assigned to c, which overwrites the value of 1 that was just there.
I hope this helps,
Corey


SCJP Tipline, etc.
luis meira
Ranch Hand

Joined: Apr 18, 2002
Posts: 55
Originally posted by Corey McGlone:
It's important to remember what's in the variable c while each piece of the expression is being evaluated. Let's go through it, step by step.
The code as is:

The first thing the JVM is going to do is evaluate each argument in the expression. I'll do this piece by piece and show what c is at each step. Let's start with the fact that c += c++ is equivalent to c = c + c++.

As you can see, in the line labelled "Look here" the expression c++ is evaluated to 0 and then c is incremented to 1. Now, however, if you continue with the evaluation of the expression, the right hand side evaluates to 0. This value is then assigned to c, which overwrites the value of 1 that was just there.
I hope this helps,
Corey


now i think i got it.
you've made me see the light.
many thanks Corey,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Basics