DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Assignment Operators in Java

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:

  1. Simple Assignment (=)
  2. Chained Assignment (= = =)
  3. Compound Assignment (+=, -=, *=, /=, etc.)

1. Simple Assignment

The simple assignment operator (=) assigns a value to a variable.

int x = 10;

System.out.println(x);
Enter fullscreen mode Exit fullscreen mode

Output

10
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

20 --- 20 --- 20 --- 20
Enter fullscreen mode Exit fullscreen mode

How Does Chained Assignment Work?

Assignments are evaluated from right to left.

a = b = c = d = 20
Enter fullscreen mode Exit fullscreen mode

Execution order:

d = 20

↓

c = d

↓

b = c

↓

a = b
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Here,

  • b, c, and d are already declared.
  • Only a is being declared.

Invalid Chained Assignment

int a = b = c = d = 30;
Enter fullscreen mode Exit fullscreen mode

Compiler Error

cannot find symbol

symbol: variable b
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

you can write

a += 5;
Enter fullscreen mode Exit fullscreen mode

Both produce the same result.


Example

int marks = 80;

marks += 20;

System.out.println(marks);
Enter fullscreen mode Exit fullscreen mode

Output

100
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Compiler Error

possible lossy conversion from int to byte
Enter fullscreen mode Exit fullscreen mode

Why?

The expression

b + 1
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

11
Enter fullscreen mode Exit fullscreen mode

The compiler performs the required cast automatically.


Case 3: Compound Assignment

byte b = 10;

b += 1;

System.out.println(b);
Enter fullscreen mode Exit fullscreen mode

Output

11
Enter fullscreen mode Exit fullscreen mode

This also works because compound assignment performs an implicit cast.


What Does the Compiler Actually Do?

The statement

b += 1;
Enter fullscreen mode Exit fullscreen mode

is treated as

b = (byte)(b + 1);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

-126
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

-160 --- -180 --- 200 --- 10
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Execution

Initial values

a = 20
b = 20
c = 20
d = 20
Enter fullscreen mode Exit fullscreen mode

Step 1

d /= 2;
Enter fullscreen mode Exit fullscreen mode
d = 10
Enter fullscreen mode Exit fullscreen mode

Step 2

c *= d;
Enter fullscreen mode Exit fullscreen mode
20 × 10 = 200

c = 200
Enter fullscreen mode Exit fullscreen mode

Step 3

b -= c;
Enter fullscreen mode Exit fullscreen mode
20 - 200 = -180

b = -180
Enter fullscreen mode Exit fullscreen mode

Step 4

a += b;
Enter fullscreen mode Exit fullscreen mode
20 + (-180) = -160

a = -160
Enter fullscreen mode Exit fullscreen mode

Final values

a = -160

b = -180

c = 200

d = 10
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Three Ways to Add 1 to a byte

byte b = 10;
Enter fullscreen mode Exit fullscreen mode

Method 1

b = b + 1;
Enter fullscreen mode Exit fullscreen mode

❌ Compile-time error


Method 2

b++;
Enter fullscreen mode Exit fullscreen mode

✅ Works


Method 3

b += 1;
Enter fullscreen mode Exit fullscreen mode

✅ 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;
Enter fullscreen mode Exit fullscreen mode

is internally treated as

b = (byte)(b + 1);
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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 += 1 works with byte, while b = b + 1 results in a compile-time error because the expression is promoted to int.
  • 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)