Java Fundamentals  «Prev 

Arithmetic Compound Assignment Operators

Java provides special operators that can be used to combine an arithmetic operation with an assignment. As you probably know, statements like the following are quite common in programming:
a = a + 4;

In Java, you can rewrite this statement as shown here:
a += 4;

This version uses the += compound assignment operator. Both statements perform the same action: they increase the value of a by 4. Here is another example,
a = a % 2;

which can be expressed as
a %= 2;

In this case, the %= obtains the remainder of a /2 and puts that result back into a. There are compound assignment operators for all of the arithmetic, binary operators. Thus, any statement of the form
var = var op expression;

can be rewritten as
var op= expression;
The compound assignment operators provide two benefits. First, they save you a bit of typing, because they are “shorthand” for their equivalent long forms. Second, in some cases they are more efficient than are their equivalent long forms. For these reasons, you will often see the compound assignment operators used in professionally written Java programs.
Java Reference

Compound Assignment Operator

1) Simple assignment: Assigns a value to a variable

2) Addition: Adds a value to a variable and assigns the result to the variable

3) Subtraction: Subtracts a value from a variable and assigns the result to the variable

4) Multiplication: Multiplies a variable by a value and assigns the result to the variable

5) Division: Divides a variable by a value and assigns the result to the variable

6) Modulus: Dividies a variable by a value and assigns the remainder to the variable