Learn Java
import com.learnjava.operators.OpAssignment;

Assignment Operators in Java

@Concept

Beyond plain =, Java has compound assignment operators that combine an operation with assignment in one step: +=, -=, *=, /=, and %= each apply the operator to the variable's current value and store the result back into that same variable. x += 5 means exactly the same thing as x = x + 5, just shorter to write — and, for some types, subtly different underneath.

On a narrower type like byte or short, compound assignment includes an implicit narrowing cast that plain arithmetic wouldn't get. byteVar += 1 compiles cleanly even though byteVar = byteVar + 1 would not, since the right side of that second version promotes to int and can't be assigned back to byte without an explicit cast.

@HowItWorks
1

Plain assignment

= stores the value on the right into the variable on the left.

2

Compound arithmetic assignment

+=, -=, *=, /=, %= each combine an operation with storing the result back into the same variable.

3

Compound assignment is shorthand

x += 5 does exactly what x = x + 5 does, just more compactly.

4

A hidden implicit cast

On a narrower type like byte or short, a compound assignment operator includes an implicit narrowing cast that writing the expanded form out by hand would not get automatically.

5

Compound bitwise assignment exists too

&=, |=, ^=, <<=, >>=, and >>>= apply the matching bitwise operator the same shorthand way.

@Example
public class AssignmentDemo {
public static void main(String[] args) {
int total = 10;
total += 5;
total *= 2;
System.out.println("Total: " + total);
 
byte small = 10;
small += 1;
System.out.println("Small: " + small);
}
}
Output
Total: 30
Small: 11
1total += 5 adds 5 to total, making it 15.
2total *= 2 then doubles that, making it 30.
3small += 1 compiles cleanly on a byte thanks to the implicit narrowing cast built into compound assignment, becoming 11.
4Both println calls run once each, confirming both the plain arithmetic chain and the byte case landed on the expected values.
@UseCases
Accumulating a running total or count inside a loop
Writing byteVar += 1 instead of the more verbose, cast-requiring byteVar = (byte) (byteVar + 1)
Toggling individual bits with compound bitwise assignment, like flags ^= mask
Making repeated arithmetic updates to a variable more compact and readable

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.