# 3.3. Changing Variables in Java¶

Remember that a variable holds a value and that value can change or vary. If you use a variable to keep score you would probably increment it (add one to the current value). You can do this by setting the variable to the current value of the variable plus one (score = score + 1) as shown below.

You can set one variable’s value to a copy of the value of another variable. This won’t change the value of the variable that you are copying from. Step through the code below by clicking the “Forward” button to see how the values of the variables change.

3-3-1: What are the values of x, y, and z after the following code executes? You can step through this code by clicking on the following link

```int x = 0;
int y = 1;
int z = 2;
x = y;
y = y * 2;
z = 3;
```
• x = 0, y = 1, z = 2
• These are the initial values in the variable, but the values are changed.
• x = 1, y = 2, z = 3
• x changes to y's initial value, y's value is doubled, and z is set to 3
• x = 2, y = 2, z = 3
• Remember that the equal sign doesn't mean that the two sides are equal. It sets the value for the variable on the left to the value from evaluating the right side.
• x = 0, y = 0, z = 3
• Remember that the equal sign doesn't mean that the two sides are equal. It sets the value for the variable on the left to the value from evaluating the right side.

Mixed up programs

```        3-3-2: The following has the correct code to 'swap' the values in x and y (so that x ends up with y's initial value and y ends up with x's initial value), but the code is mixed up and contains one extra block which is not needed in a correct solution.  Drag the needed blocks from the left into the correct order on the right. Check your solution by clicking on the Check Me button.  You will be told if any of the blocks are in the wrong order or if you need to remove one or more blocks.  After three incorrect attempts you will be able to use the Help Me button to make the problem easier.int x = 3;
int y = 5;
int temp = 0;
---
temp = x;
---
x = y;
---
y = temp;
---
y = x; #distractor
```