aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Self decrement question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Self decrement question" Watch "Self decrement question" New topic
Author

Self decrement question

Tse Wu
Greenhorn

Joined: Jan 05, 2012
Posts: 10
Hi, why is the following 1?

int y = 1;
y = y-- + y--;

Thanks.
matt love
Ranch Hand

Joined: Jan 25, 2010
Posts: 65
Hi Tse,

to resolve what’s on the right of the equals:
1: obtain the value of y first which is 1 before decrimenting (since this is a post-decriment),
2: then decriment y by –1 to 0,
3: again since you are post-decrimenting, obtain the value of y first, which is now 0,
4: decriment y to –1;

so, y = 1 (from step 1 above) + 0 (from step 3 above) = 1

notice the last decriment had no effect on the equation.

Matt
Tse Wu
Greenhorn

Joined: Jan 05, 2012
Posts: 10
Thanks Matt.
Y ended up not being -1 due to the fact that the final value was assigned as the sum of the expression. Essentially y was -1 before being reassigned to 1.. Do I have the jist of it?
matt love
Ranch Hand

Joined: Jan 25, 2010
Posts: 65
Hi Tse.

I didn't quite follow your response.

If you are willing, let's take it step-by-step, with separate emails for each step, and see if there is a particular step that you're having difficulty with.

int y = 1;
y = y-- + y--;

we will resolve the expression left-to-right, so we will first address the first y--.

i believe y-- is called a post-decriment operator because we will evaluate y (or another phrase might be to obtain the value of y) first and then decriment y by 1.

--y would be called a pre-decriment operator because we would decriment y before we would evaluate and obtain the value of y.

so, back to our expression: we have evaluated and retained the value of y as 1.

we are still working with the leftmost y-- and we haven't decrimented it yet.

ok so far?

Matt



Tse Wu
Greenhorn

Joined: Jan 05, 2012
Posts: 10
Hi Matt,
From my understanding.

Step1: Since the first y is post decrement, the first y value is 1.
Step2: After the resolution of the first y, y is decreased to 0.
Step3: The second y is also post decrement, so the second y value is assigned to 0.
Step4: after the resolution of the second y, y is decreased to -1.
Step5: so we ended up with 1 + 0
Step6: At this point y is -1, but since 1 + 0 is 1 the assignment of y changed from -1 to 1 at this point.

I think this is how it goes with the explanation you provided which made sense. Let me know if one of the steps was wrong. Thanks again.
matt love
Ranch Hand

Joined: Jan 25, 2010
Posts: 65
You have it correct to the end where you're a little off.

int y = 1;
y = y-- + y--;

Step1: Since the first y is post decrement, the first y value is 1.

yes, y is evaluated as having value 1.

Step2: After the resolution of the first y, y is decreased to 0.

Yes

Step3: The second y is also post decrement, so the second y value is assigned to 0.

correct

Step4: after the resolution of the second y, y is decreased to -1.

yep

Step5: so we ended up with 1 + 0

yep

Step6: At this point y is -1, but since 1 + 0 is 1 the assignment of y changed from -1 to 1 at this point.

I think you have the concept down Tse, although y did not change from -1 to 1 at the end. It’s more a matter of snapshots in time. As far as the expression is concerned, y resolves to 1 at the end because of the progression you described.

After the expression, y is in fact –1, because of that final post-decrementer. That second y-- evaluated to 0 so the expression could be resolved (it is a post-decrementer) and then that second y-- decremented y by 1.

int y = 1;
y = y-- + y--;
System.out.println(y); // y is –1 at this point after the above entire expression is executed, including that final post-decrement;

That final decrement, --, has the final effect on the value of y after the expression was completely and finally completed. Remember, the value of y in the second y—was evaluated before it was decremented.

I know it looks weird. Be sure and let me know if you’re still hung up on something and I’ll respond tomorrow.

Matt


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Self decrement question
 
Similar Threads
Operator Evaluation
Another Puzzle
Operator precedence
Dream vacation
JDK API Download