Java Reference

In-Depth Information

Changing Variables

If a variable has been assigned a numerical value, it can be increased using the following

operation:

points = 0; // initialize points score to zero

<< 0

points = points + 10;

<< 10

This will increase the value held in the
points
variable by
10
. You can also use the com-

pound assignment operator,
+=
, which is a shortcut for performing the same task, but helps

you avoid writing the variable name twice:

points += 10;

<< 20

There are equivalent compound assignment operators for all the operators in the previous

section:

points -= 5; // decreases points by 5

<< 15

points *= 2; // doubles points

<< 30

points /= 3; // divides value of points by 3

<< 10

points %= 7; // changes the value of points to the

remainder

↵
if its current value is divided by 7

<< 3