Assignment operators are among the most commonly used operators in Java. While the simple assignment operator (=) is familiar to every programmer, Java also provides chained assignment and compound assignment operators that can make your code shorter and more expressive.
In this guide, you'll learn:
- Simple assignment
- Chained assignment
- Compound assignment
- Internal type casting in compound assignments
- Overflow behavior
- Evaluation order
- Common interview questions
- Memory tricks
What are Assignment Operators?
Assignment operators are used to assign values to variables.
Java supports three types of assignment operators:
- Simple Assignment (
=) - Chained Assignment (
= = =) - Compound Assignment (
+=,-=,*=,/=, etc.)
1. Simple Assignment
The simple assignment operator (=) assigns a value to a variable.
int x = 10;
System.out.println(x);
Output
10
This is the most basic form of assignment.
2. Chained Assignment
Chained assignment allows multiple variables to receive the same value in a single statement.
Example
int a, b, c, d;
a = b = c = d = 20;
System.out.println(a + " --- " + b + " --- " + c + " --- " + d);
Output
20 --- 20 --- 20 --- 20
How Does Chained Assignment Work?
Assignments are evaluated from right to left.
a = b = c = d = 20
Execution order:
d = 20
↓
c = d
↓
b = c
↓
a = b
All variables finally store the value 20.
Chained Assignment During Declaration
This is also valid.
int b, c, d;
int a = b = c = d = 20;
Here,
-
b,c, anddare already declared. - Only
ais being declared.
Invalid Chained Assignment
int a = b = c = d = 30;
Compiler Error
cannot find symbol
symbol: variable b
Why?
Because b, c, and d have not been declared yet.
The compiler processes declarations from left to right, so those variables don't exist at that point.
Valid vs Invalid
| Code | Valid? | Reason |
|---|---|---|
int a, b, c, d; a = b = c = d = 20; |
✅ | All variables already declared |
int b, c, d; int a = b = c = d = 20; |
✅ | Only a is newly declared |
int a = b = c = d = 20; |
❌ |
b, c, and d are undeclared |
3. Compound Assignment
Compound assignment combines an operator with the assignment operator.
Instead of writing
a = a + 5;
you can write
a += 5;
Both produce the same result.
Example
int marks = 80;
marks += 20;
System.out.println(marks);
Output
100
Compound Assignment Operators
| Operator | Equivalent |
|---|---|
+= |
a = a + b |
-= |
a = a - b |
*= |
a = a * b |
/= |
a = a / b |
%= |
a = a % b |
&= |
a = a & b |
|= |
a = a| b |
^= |
a = a ^ b |
<<= |
a = a << b |
>>= |
a = a >> b |
>>>= |
a = a >>> b |
The Most Important Feature of Compound Assignment
Compound assignment performs automatic type casting.
This is one of the most frequently asked Java interview topics.
Case 1: Regular Assignment
byte b = 10;
b = b + 1;
Compiler Error
possible lossy conversion from int to byte
Why?
The expression
b + 1
is promoted to an int.
Java does not automatically convert the result back to byte.
Case 2: Increment Operator
byte b = 10;
b++;
System.out.println(b);
Output
11
The compiler performs the required cast automatically.
Case 3: Compound Assignment
byte b = 10;
b += 1;
System.out.println(b);
Output
11
This also works because compound assignment performs an implicit cast.
What Does the Compiler Actually Do?
The statement
b += 1;
is treated as
b = (byte)(b + 1);
The cast is inserted automatically by the compiler.
This behavior is similar to the increment (++) and decrement (--) operators.
Overflow Example
Automatic casting does not prevent overflow.
byte b = 127;
b += 3;
System.out.println(b);
Output
-126
Why?
A byte can store values only from -128 to 127.
Adding 3 exceeds the maximum value, causing overflow.
Evaluation Order of Compound Assignment
Consider this interview question.
int a, b, c, d;
a = b = c = d = 20;
a += b -= c *= d /= 2;
System.out.println(a + " --- " + b + " --- " + c + " --- " + d);
Output
-160 --- -180 --- 200 --- 10
Step-by-Step Execution
Initial values
a = 20
b = 20
c = 20
d = 20
Step 1
d /= 2;
d = 10
Step 2
c *= d;
20 × 10 = 200
c = 200
Step 3
b -= c;
20 - 200 = -180
b = -180
Step 4
a += b;
20 + (-180) = -160
a = -160
Final values
a = -160
b = -180
c = 200
d = 10
Why Does This Happen?
Assignment operators are right-associative.
Java evaluates them from right to left.
a += b -= c *= d /= 2
↓
d /= 2
↓
c *= d
↓
b -= c
↓
a += b
Three Ways to Add 1 to a byte
byte b = 10;
Method 1
b = b + 1;
❌ Compile-time error
Method 2
b++;
✅ Works
Method 3
b += 1;
✅ Works
Comparison
| Statement | Works? | Reason |
|---|---|---|
b = b + 1; |
❌ | Result becomes int
|
b++; |
✅ | Compiler performs internal cast |
b += 1; |
✅ | Compound assignment performs internal cast |
Summary Table
| Assignment Type | Example | Description |
|---|---|---|
| Simple | int x = 10; |
Assign a value |
| Chained | a = b = c = 20; |
Assign the same value to multiple variables |
| Compound | a += 5; |
Combines an operation with assignment |
Interview Questions
How many types of assignment operators are there in Java?
Three:
- Simple Assignment
- Chained Assignment
- Compound Assignment
In which direction is chained assignment evaluated?
Right to left.
Does compound assignment perform automatic type casting?
Yes.
For example,
b += 1;
is internally treated as
b = (byte)(b + 1);
Why does b = b + 1; fail for a byte?
Because b + 1 is promoted to an int, and Java does not automatically narrow it back to a byte.
Can compound assignment still overflow?
Yes.
Automatic casting does not prevent overflow.
Memory Tricks 🧠
Simple Assignment
Assign a value.
x = 10;
Chained Assignment
One value, many variables.
Evaluated right to left.
Compound Assignment
Do the operation and assign the result.
The compiler automatically inserts the required cast.
Easy Way to Remember
Regular assignment is strict. Compound assignment is smart.
-
b = b + 1→ Needs an explicit cast. -
b += 1→ Compiler adds the cast automatically.
Key Takeaways
- Java supports simple, chained, and compound assignment operators.
- Chained assignments are evaluated from right to left.
- Compound assignment operators (
+=,-=,*=, etc.) automatically perform the required type casting. -
b += 1works withbyte, whileb = b + 1results in a compile-time error because the expression is promoted toint. - Automatic casting in compound assignments does not prevent overflow.
- Understanding assignment operator behavior is important for Java interviews and writing concise, correct code.
Happy Coding!
Top comments (0)