Java Assignment Operators
Assign values to variables with =, +=, -=, *=, /=, %=, and the compound bitwise assignment operators.
The single = is the basic assignment operator. Java also provides a family of compound assignment operators that combine an arithmetic, bitwise, or shift operation with the assignment itself. They save a few characters and make the intent — "modify this variable in place" — clearer.
Plain assignment
The right-hand side is evaluated, then stored into the left-hand variable:
int x = 5;
int y = x + 3;
y = y * 2;
System.out.println(y); // 16A few rules:
- The left-hand side must be a variable (or array slot, or field), never an expression like
(x + 1) = 5. - The types must match — or the right-hand side must be implicitly convertible to the left-hand type.
- Assignment is itself an expression:
int a = (b = 5);sets bothaandbto5.
Compound assignment operators
Each arithmetic and bitwise operator has a paired compound assignment form:
| Operator | Long form |
|---|---|
+= | x = x + value |
-= | x = x - value |
*= | x = x * value |
/= | x = x / value |
%= | x = x % value |
&= | x = x & value |
| ` | =` |
^= | x = x ^ value |
<<= | x = x << value |
>>= | x = x >> value |
>>>= | x = x >>> value |
In practice:
int total = 0;
total += 5; // total = 5
total += 3; // total = 8
total *= 2; // total = 16
total -= 1; // total = 15
total /= 3; // total = 5+= with strings
+= also concatenates onto a string:
String greeting = "Hello";
greeting += ", world!"; // "Hello, world!"Each += on a String creates a new string under the hood — strings are immutable. For loops that build up a long string, use StringBuilder instead (covered in Java String Concatenation).
Compound assignment includes an implicit cast
A subtle but important quirk: compound assignment operators include an implicit cast back to the variable's type. Plain assignment does not.
byte b = 10;
// b = b + 1; // compile error: int can't fit into byte without a cast
b += 1; // OK — compound form casts implicitly
System.out.println(b); // 11This is occasionally useful — and occasionally a silent foot-gun if it hides an overflow.
Assignment is an expression — but don't abuse it
Because assignment yields a value, you can chain it:
int a, b, c;
a = b = c = 10; // all three set to 10You can also assign inside an if test:
String line;
while ((line = reader.readLine()) != null) {
process(line);
}These idioms are common in I/O loops. Outside of while ((x = ...) != null), prefer separate statements — they're easier to read.
= vs ==
The single biggest source of one-character bugs in Java is mixing up = (assignment) and == (comparison):
int x = 5;
// if (x = 10) { ... } // compile error: int is not a boolean
// if (x == 10) { ... } // OKFor int and most types the compiler catches the mistake immediately because = returns an int, not a boolean. With boolean variables there's no compile error — assignment with = always returns the assigned boolean — so be especially careful when testing flags:
boolean isOpen = false;
if (isOpen = true) { ... } // bug: this sets isOpen and is always true
if (isOpen == true) { ... } // works, but...
if (isOpen) { ... } // ...this is what you actually wantA demonstration
What's next
Java Comparison Operators covers ==, !=, the relational operators, and the crucial difference between == and .equals() for objects.
Practice
What does this code print?\n\nint x = 10;\nx += 5;\nx *= 2;\nSystem.out.println(x);