Compound-Assignment Operators

Businesswoman working on laptop
Atanas Bezov/E+/Getty Images

Compound-assignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. They perform the operation on the two operands before assigning the result to the first operand.

Compound-Assignment Operators in Java

Java supports 11 compound-assignment operators:

+=   assigns the result of the addition.
-=   assigns the result of the subtraction.
*=   assigns the result of the multiplication
/=   assigns the result of the division.
%=   assigns the remainder of the division.
&=   assigns the result of the logical AND.
|=   assigns the result of the logical OR.
^=   assigns the result of the logical XOR.
<<=  assigns the result of the signed left bit shift.
>>=  assigns the result of the signed right bit shift.
>>>= assigns the result of the unsigned right bit shift.

Example Usage

To assign the result of an addition operation to a variable using the standard syntax:

//add 2 to the value of number
number = number + 2;

But use a compound-assignment operator to effect the same outcome with the simpler syntax:

//add 2 to the value of number
number += 2;
Format
mla apa chicago
Your Citation
Leahy, Paul. "Compound-Assignment Operators." ThoughtCo, Feb. 16, 2021, thoughtco.com/compound-assignment-operator-2034054. Leahy, Paul. (2021, February 16). Compound-Assignment Operators. Retrieved from https://www.thoughtco.com/compound-assignment-operator-2034054 Leahy, Paul. "Compound-Assignment Operators." ThoughtCo. https://www.thoughtco.com/compound-assignment-operator-2034054 (accessed April 22, 2021).