Compound Assignment
Operators
Master the shorthand operators that combine arithmetic and assignment into a single, elegant expression in Java.
What Are Compound Assignment Operators?
A compound assignment operator combines an arithmetic operation with the assignment operator (=) into a single step. Instead of writing the variable name twice, you write it once.
The Five Compound Operators
Why Use Compound Operators?
count += 1 clearly says "increment count," while count = count + 1 forces you to parse both sides.The General Rule
Hidden Feature: Implicit Casting
Compound assignment operators include an implicit type cast to the variable's type. This is a subtle but important difference from the expanded form.
+= vs = ... +: Key Differences
| \( \textcolor{#818cf8}{x \mathrel{+}= \text{expr}} \) | \( \textcolor{#f87171}{x = x + \text{expr}} \) | |
|---|---|---|
| Characters typed | Fewer (variable name once) | More (variable name twice) |
| Implicit cast? | Yes: casts result to type of x | No: may cause compile error |
| Variable evaluated | Once | Twice |
| Behavior | Identical result when types match | Identical result when types match |
| AP Exam | Expected | Also valid |
How to Apply Compound Operators
int and the expression is double, the compound form will truncate. Make sure that's intended.Example 1: Basic Conversion
Convert each expanded statement to its compound form.
Example 2: Tracing Values
Trace the value of x after each line executes:
Example 3: Loop Accumulation
Compound operators shine in loops. Here we accumulate a running total:
Example 4: String Concatenation with +=
The += operator also works with Strings. It appends text:
Watch the Order of Operations
The entire right-hand expression is evaluated first, then the operation is applied. This matters with complex expressions:
How x += 5 Executes
Common Mistakes
+ = (with a space) instead of +=. The operator must be a single token: no space between + and =.=+ instead of +=. In Java, x =+ 5 assigns positive 5 to x (unary +), not x + 5.x *= 2 + 3 means (x*2) + 3. It actually means x * (2+3). The full right side is evaluated first.y += 10; before declaring y. The variable must exist and be initialized before a compound assignment.Summary Reference Table
| Operator | Example | Equivalent | Result (x=20) |
|---|---|---|---|
| += | \( x \mathrel{+}= 5 \) | \( x = x + 5 \) | 25 |
| -= | \( x \mathrel{-}= 8 \) | \( x = x - 8 \) | 12 |
| *= | \( x \mathrel{*}= 3 \) | \( x = x * 3 \) | 60 |
| /= | \( x \mathrel{/}= 6 \) | \( x = x / 6 \) | 3 (int division) |
| %= | \( x \mathrel{\%}= 7 \) | \( x = x \% 7 \) | 6 |
Question 1 of 5
What is the value of x after the following code executes?int x = 12; x += 8;
Question 2 of 5
What is the value of y after this code runs?int y = 25; y %= 7;
Question 3 of 5
What does \( x \mathrel{*}= 3 + 2 \) evaluate to when \(x\) is currently 4?
Question 4 of 5
Which of the following is NOT equivalent to \( \text{count} = \text{count} - 1; \)?
Question 5 of 5
After the following code, what is the value of n?int n = 100; n /= 3; n *= 3;