You're already familiar with the assignment operator (=) that
we use to assign values to variables. There are also some
augmented assignment operators that you can use to perform
simple mathematical operations and assign the results, using
a single operator.
There are some special assignment operators you can use as shortcuts to
statements that need to perform a simple mathematical operation
and assign the result to a variable. To understand how these can be used,
look at the program listing below. What is the output of this program? What is
the value of the variable sum during execution of this program?
public class AssignOps {
public static void main(String[] args) {
int sum = 10;
System.out.println("The value stored in sum is " + sum);
sum = sum + 20;
System.out.println("The value now stored in sum is " + sum);
}
}
This small program demonstrates a situation where you can use the special
+= augmented assignment operator. This operator,
called the "plus-equals" operator, adds
the operand on the right to the operand on the left. If you rewrote the
program using the += operator, you'd have:
public class AssignOps {
public static void main(String[] args) {
int sum = 10;
System.out.println("The value stored in sum is " + sum);
sum += 20;
System.out.println("The value now stored in sum is " + sum);
}
}
There are also similar operators to perform subtraction,
multiplication, division, and modulus:
Augmented Assignment Operators
Operation
Symbol
Example
Plus-Equals
+=
sum += x; // adds x to sum and puts result back in sum
Minus-Equals
-=
y -= x; // subtracts x from y and puts result back in y
Times-Equals or Multiply-Equals
*=
y *= x; // multiplies y times x and puts result back in y
Divide-Equals
/=
y /= x; // divides y by x and puts result back in y
Modulus-Equals or Remainder-Equals
%=
y %= x; // puts the remainder from y / x into y
Note that the augmented assignment operators will not work if
the left-hand operand has not been initialized to a value.
For example, the following is invalid and will not compile:
int x;
x += 2;
To fix this example, make sure the variable x is
initialized to a value:
int x = 0;
x += 2;
Exercises
1. Examine the code listing below. What do you think the output should be? Write this down. Then run the program and see if you're correct.
public class Question1
{
public static void main(String[] args)
{
int factor = 2;
int sum = 10;
System.out.println("sum is " + sum);
sum *= factor;
System.out.println("sum is now " + sum);
sum *= factor;
System.out.println("sum is now " + sum);
sum *= factor;
System.out.println("sum is now " + sum);
}
}
Trace Chart:
Line #
Variables
Output
5
factor: 2
--
6
sum: 10
--
7
--
sum is 10
8
sum: 10 * 2 = 20
--
9
--
sum is now 20
10
sum: 20 * 2 = 40
--
11
--
sum is now 40
12
sum: 40 * 2 = 80
--
13
--
sum is now 80
Output should be:
sum is 10
sum is now 20
sum is now 40
sum is now 80
2. How do you think the output would change if you wrote
the program in question 2 like this:
public class Question2
{
public static void main(String[] args)
{
int factor = 2;
int sum = 10;
System.out.println("sum is " + sum);
sum *= factor;
sum *= factor;
sum *= factor;
System.out.println("sum is now " + sum);
System.out.println("sum is now " + sum);
System.out.println("sum is now " + sum);
}
}
Trace Chart:
Line #
Variables
Output
5
factor: 2
--
6
sum: 10
--
7
--
sum is 10
8
sum: 10 * 2 = 20
--
9
sum: 20 * 2 = 40
--
10
sum: 40 * 2 = 80
--
11
--
sum is now 80
12
--
sum is now 80
13
--
sum is now 80
Output will be:
sum is 10
sum is now 80
sum is now 80
sum is now 80
3. Determine, without coding a program, the output of the
following code segment: