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:
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:
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:
…which is identical to the previous example. Okay, now to the code!
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