Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Harnessing the Power of Objects Incrementing and Decrementing

At 2:06, why is example += 1 stored in res1 instead of example?

The variable example was stored back into itself when 1 was added to it the first time. Why is it stored in res1 after adding 1 to it again?

1 Answer

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hello Stephanie,

Far be it from me to speak on behalf of Craig.

But I think he was wanting to keep example the same and not change it's value and instead store them in other variables so that he could show things such as post increment, and pre-increment and what the difference between them was.

This way he could show people how to increment and decrements without having to reset the example back to it's initial value after each demonstration of increment or decrementing the integer.

That makes sense from a Craig point of view, but looking at the code, at 1:33, the REPL returns example as 2. Why, then, at 2:00, is 3 stored in res1 instead of example? They're basically the same thing (example = example + 1 and example++), why are they stored in 2 different variables?

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

I would assume because Craig still wanted to to keep the variables different after teaching the basic foundations of incrementing and post incrementing.

He'd be the one to know the real reason of it, but safe to assume he did it to keep easy track of what topics the increment and decrement logic that he was trying to show throughout multiple variables. So you we can see the difference between post incrementing like example++ and ++example.