Learn Java the Hard Way (Second Edition)

Exercise 11: Variable Modification Shortcuts

The value of a variable can change over time as your program runs. (It won’t change unless you write code to change it, but it can change is what I’m saying.)

In fact, this is pretty common. Something we do pretty often is take a variable and add something to it. For example, let’s say the variable x contains the value 10. We want to add 2 to it so that x now contains 12.

We can do this:

int x = 10, tempX;   // x is 10, tempX is undefined
tempX = x + 2;       // x is still 10, tempX is now 12
x = tempX;           // x has been changed to 12

This will work, but it is annoying. If we want, we can take advantage of the fact that a variable can have one value at the beginning of a line of code and have a different value stored in it by the end. So we can write something like this:

int y = 10;    // y is 10
y = 2 + y;     // y *becomes* (2 plus the current value of y)

This also works. That second line says “take the current value of y (10), add 2 to it, and store the sum (12) into the variable y. So when the second line of code begins executing, y is 10, and when it is done executing, y is 12. The order of adding doesn’t matter, so we can even do something like this:

int m = 10;
m = m + 2;

…which is identical to the previous example. Okay, now to the code!

 
 1 public class VariableChangeShortcuts {
 2     public static void main( String[] args ) {
 3         int i, j, k;
 4 
 5         i = 5;
 6         j = 5;
 7         k = 5;
 8         System.out.println( "i: " + i + "\tj: " + j + "\tk: " + k );
 9         i =  i + 3;
10         j =  j - 3;
11         k =  k * 3;
12         System.out.println( "i: " + i + "\tj: " + j + "\tk: " + k + "\n" );
13 
14         i = 5;
15         j = 5;
16         k = 5;
17         System.out.println( "i: " + i + "\tj: " + j + "\tk: " + k );
18         i += 3;
19         j -= 3;
20         k *= 3;
21         System.out.println( "i: " + i + "\tj: " + j + "\tk: " + k + "\n" );
22 
23         i = j = k = 5;
24         System.out.println( "i: " + i + "\tj: " + j + "\tk: " + k );
25         i += 1;
26         j -= 2;
27         k *= 3;
28         System.out.println( "i: " + i + "\tj: " + j + "\tk: " + k + "\n" );
29 
30         i = j = 5;
31         System.out.println( "i: " + i + "\tj: " + j );
32         i =+ 1; // Oops!
33         j =- 2;
34         System.out.println( "i: " + i + "\tj: " + j + "\n" );
35 
36         i = j = 5;
37         System.out.println( "i: " + i + "\tj: " + j );
38         i++;
39         j--;
40         System.out.println( "i: " + i + "\tj: " + j );
41     }
42 }

What You Should See

Hopefully lines 1-17 are nice and boring. We create three variables, give them values, display them, change their values and print them again. Then starting on line 14 we give the variables the same values they started with and print them.

On line 18 we see something new: a shortcut called a “compound assignment operator.” The i += 3 means the same as i = i + 3: “take the current value of i, add 3 to it, and store the result as the new value of i. When we say it out loud, we would say “i plus equals 3.”

On line 19 we see -= (“minus equals”), which subtracts 3 from j, and the next line demonstrates *=, which multiplies. There is also /=, which divides whatever variable is on the left-hand side by whatever value the right-hand side ends up equaling. (“Modulus equals” (%=) also exists, which sets the variable on the left-hand side equal to whatever the remainder is when its previous value is divided by whatever is on the right. Whew.)

Then on line 23 I do something else weird. Instead of taking three lines of code to set i, j and k all to 5, I do it in one line. (Some people don’t approve of this trick, but I think it’s fine in cases like this.) This line means “Put the value 5 into the variable k. Then take a copy of whatever value is now in k (5) and store it into j. Then take a copy of whatever is now in j and store it into i.” So when this line of code is done, all three variables have been changed to equal 5.

Lines 25 through 27 are basically the same as lines 18 through 20 except that we are no longer using 3 as the number to add, subtract or multiply.

Line 32 might look like a typo, and if you wrote this in your own code it probably would be. Notice that instead of += I wrote =+. This will compile, but it is not interpreted the way you’d expect. The compiler sees i = +1;, that is, “Set i equal to positive 1.” And line 33 is similar: “Set j equal to negative 2.” So watch for that.

On line 38 we see one more shortcut: the “post-increment operator.” i++ just means “add 1 to whatever is in i.” It’s the same as writing i = i + 1 or i += 1. Adding 1 to a variable is super common. (You’ll see.) That’s why there’s a special shortcut for it.

And finally on the next line we see the “post-decrement operator”: j--. It subtracts 1 from the value in j.

Today’s lesson is unusual because these shortcuts are optional. You could write code your whole life and never use them. But most programmers are lazy and don’t want to type any more than they have to, so if you ever read other people’s code you will see these pretty often.

Especially i++. You will see that all the time.

(There is no video for these Study Drills yet.)


“Learn Java the Hard Way” is ©2013–2016 Graham Mitchell