Assignment Operator in Java Explained with Examples (2026 Guide)
The assignment operator in Java is used to assign a value to a variable. While the basic = operator is the most familiar, Java also offers several compound assignment operators that combine an operation with assignment in a single step.
The Simple Assignment Operator (=)
Assigns the value on the right-hand side to the variable on the left-hand side.
int age = 25;
Compound Assignment Operators
These operators perform an operation and assign the result back to the same variable, making code shorter and often clearer.
Addition Assignment (+=)
int total = 10;
total += 5; // same as total = total + 5, now 15
Subtraction Assignment (-=)
int stock = 20;
stock -= 4; // now 16
Multiplication Assignment (*=)
int price = 100;
price *= 2; // now 200
Division Assignment (/=)
int value = 50;
value /= 5; // now 10
Modulus Assignment (%=)
int num = 17;
num %= 5; // now 2
Bitwise Compound Assignment Operators
Java also supports compound versions of bitwise operators, such as &=, |=, ^=, <<=, and >>=, which apply a bitwise operation and reassign the result.
=— assigns a value+=,-=,*=,/=,%=— arithmetic compound assignment&=,|=,^=,<<=,>>=— bitwise compound assignment
Compound assignment operators aren't just shorter to type — they also implicitly cast the result back to the variable's original type, which can behave differently from writing the operation out in full.
Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.