The assignment operator is one of the most fundamental operators in Java programming language. This operator is used to assign a value to a variable. The assignment operator is represented by equal (=) symbol. It is a binary operator that takes two operands. The value of the right-hand operand is assigned to the left-hand operand. The left-hand operand must be a variable. The general syntax is as follows:
Variable = value;
This operator assigns the value on the right-hand side to the variable on the left-hand side.
For example,
int num = 20;
Here, num = 20 uses the assignment operator (=). In this example, 20 is the right-hand operand, while num is the left-hand operand, which is a variable of type int. The integer value 20 is assigned to the variable named num.

Types of Assignment Operators in Java
In addition to simple assignment operator (=), Java language also supports several types of assignment operators. These include compound assignment operators, which combine arithmetic operators with assignment. Let us understand them one by one with the help of examples.
1. +=
Add and Assign
The += operator in Java is a compound assignment operator. It adds the right-hand operand to the left-hand operand and assigns the result back to the left-hand operand. The general syntax is as follows:
x += y; // Equivalent to: x = x + y;
In the above syntax, x is the variable to which the value will be added. y is the value being added to x.
Example 1:
int sum = 5;
sum += 10; // sum = sum + 10
System.out.println("Sum: " +sum);
Output:
Sum: 15
Example 2:
char ch = 'A';
ch += 1; // ch = (char)(ch + 1)
System.out.println(ch);
Output:
B
Although String is immutable data type, you can use += for string concatenation. Look at the below example code.
Example 3:
String greeting = "Hello";
greeting += " World";
System.out.println(greeting);
Output:
Hello World
2. -= Subtract and Assign
The -= operator is a compound assignment operator in Java which subtracts the right-hand operand from the left-hand operand and assigns the result back to the left-hand operand. The general syntax is as follows:
x -= y; // Equivalent to: x = x - y;
In the above syntax, x is the variable whose value will be updated. y is the value that will be subtracted from x.
Example 4:
int a = 10;
a -= 4; // same as: a = a - 4;
System.out.println(a);
Output:
6
Example 5:
char ch = 'D';
ch -= 2; // ch = (char)(ch - 2);
System.out.println(ch);
Output:
B
Example 6:
int marks = 85;
int penalty = 5;
marks -= penalty;
System.out.println("Final Marks: " + marks);
Output:
Final Marks: 80
3. *= Multiply and Assign
This compound assignment operator multiplies the left-hand variable by the right-hand value and assigns the result back to the variable. The general syntax is as follows:
x *= y; // same as: x = x * y;
Example 7:
int a = 4;
a *= 3; // a = a * 3
System.out.println(a);
Output:
12
Example 8: Java program to calculate the area of a rectangle.
int length = 5;
int width = 8;
int area = 1;
area *= length; // area = area * length (1 * 5 = 5)
area *= width; // area = area * width (5 * 8 = 40)
System.out.println("Area = " + area);
Output:
Area = 40
4. /= Divide and Assign
The divide and assign operator (/=) divides the left-hand operand by the right-hand operand and assigns the result back to the variable. The basic syntax is as:
x /= y; // same as: x = x / y;
Example 9:
double amount = 100.0;
amount /= 4;
System.out.println(amount);
Output:
25.0
Example 10: Average Calculation
int totalMarks = 450;
int subjects = 5;
totalMarks /= subjects; // totalMarks = 450 / 5 = 90
System.out.println("Average = " + totalMarks);
Output:
Average = 90
5. %= Modulo and Assign
The modulo assign %= operator calculates the remainder after division and assigns the result back to the variable. The basic syntax is as:
x %= y; // same as: x = x % y;
Example 11:
int num = 10;
num %= 3; // num = 10 % 3 = 1
System.out.println(num);
Output:
1
Example 12: Check Even or Odd
int number = 7;
number %= 2;
if (number == 0)
System.out.println("Even");
else
System.out.println("Odd");
Output:
Odd
Quiz on Assignment Operators in Java
Question 1. What will be the output of this code?
int a = 10;
a += 5;
System.out.println(a);
- a) 5
- b) 10
- c) 15
- d) 20
Answer: c) 15
Question 2. What is the difference between x = x + 1; and x += 1;?
- a) No difference
- b)
+=
is faster - c)
x += 1
only works for integers - d)
x = x + 1
gives compilation error
Answer: a) No difference
Question 3. Which operator is used to perform multiplication and assignment in one step?
- a)
=*
- b)
*=
- c)
**=
- d)
x*=
Answer: b) *=
Question 4. What is the output of the following code?
int x = 5;
x += x * 2;
System.out.println(x);
- a) 10
- b) 15
- c) 5
- d) 20
Answer: b) 15
Explanation: x += x * 2
⇒ x = x + (x * 2)
⇒ x = 5 + (5*2) = 15
Question 5. What happens when we will use divide assign operator /= with integer division?
int x = 7;
x /= 2;
- a) Runtime error
- b) Output is 3.5
- c) Output is 3
- d) Output is 4
Answer: c) Output is 3
Frequently Asked Questions on Assignment Operator in Java
1. Can we use assignment operators with different data types?
Answer: Yes, but Java performs type conversion. For example, using +=
with a char
and int
will promote the char
to int
.
2. Is x += y
more efficient than x = x + y
?
Answer: Yes, especially when working with primitive types. It can generate slightly more optimized bytecode.
3. Can assignment operators be used in chained expressions?
Answer: int a, b, c;
a = b = c = 100;
This works because the assignment operator returns the assigned value.
4. Are assignment operators overloaded in Java?
Answer: No. Java does not support operator overloading (unlike C++). The behavior is fixed and cannot be customized.
5. What is the associativity of the assignment operator?
Answer: Assignment operators are right-to-left associative, meaning: a = b = c = 10; is evaluated as: a = (b = (c = 10));