Assignment Operators in Java
11 Apr 2025 | 5 min read
In Java, the assignment operator is used to assign values to a variable. It
is denoted by the equals to symbol (=).
Types of Assignment Operators
There are the following two types of assignment operators in Java.
1. Simple Assignment Operator
2. Compound Assignment Operator
Simple Assignment Operator (=)
To assign a value to a variable, use the basic assignment operator (=). It
is the most fundamental assignment operator in Java. It assigns the right-
hand side value to the left-hand side variable.
Example:
1. int x = 10;
The above statement assigns the right-side value to the left-hand side
variable.
Compound Assignment Operator
Compound assignment operators are the combinations of basic operations
(like addition or subtraction) with assignment.
In other words, we can say that combining the arithmetic operators with
the simple assignment operator creates compound assignments. These
operators are shorthand for common operations, so it is also known as
shorthand operators.
Note: Do not change the order of the operator. It means first write
the operation symbol to perform after the assignment operator. It is
an invalid =+ compound assignment operator.
There are the following types of compound assignment operators.
1. Addition Assignment Operator
2. Subtraction Assignment Operator
3. Multiplication Assignment Operator
4. Division Assignment Operator
5. Bitwise Assignment Operator
Operator Purpose Example Equival
+= Addition a+=2 a=a+2
-= Subtraction a-=2 a=a-2
*= Multiplication a*=2 a=a*2
/= Division a/=2 a=a/2
%= Modulus a%=2 a=a%2
Addition Assignment Operator (+=)
The addition Assignment operator is used to add a value to a variable and
subsequently assign the new value to the same variable. It takes the
value on the right side of the operator, adds it to the variable's existing
value on the left side, and then assigns the new value to the variable. The
(+=) symbol denotes it. For example, consider the following program.
Example
1. public class Main {
2. public static void main(String args[]) {
3. int x = 10;
4. x += 5;
5. [Link](x);
6. }
7. }
Output:
15
Subtraction Assignment Operator (-=)
The subtraction assignment operator is used to subtract a value from a
variable and subsequently assign the new value to the same variable. It
takes the value on the right side of the operator, subtracts it from the
variable's existing value on the left side, and then assigns the new value
to the variable. The (-=) symbol denotes it. For example, consider the
following program.
Example
1. public class Main {
2. public static void main(String args[]) {
3. int x = 10;
4. x -= 5;
5. [Link](x);
6. }
7. }
Output:
Multiplication Assignment Operator (*=)
The multiplication assignment operator is used to multiply a value by a
variable and subsequently assign the new value to the same variable. It
takes the value on the right side of the operator, multiplies it by the
variable's existing value on the left side, and then assigns the new value
to the variable. The (*=) symbol denotes it. For example, consider the
following program.
Example
1. public class Main {
2. public static void main(String args[]) {
3. int x = 10;
4. x *= 5;
5. [Link](x);
6. }
7. }
Output:
50
Division Assignment Operator (/=)
The division assignment operator is used to divide a value by a variable
and subsequently assign the new value to the same variable. The (/=)
symbol denotes it. For example, consider the following program.
Example
1. public class Main {
2. public static void main(String args[]) {
3. int x = 10;
4. x /= 5;
5. [Link](x);
6. }
7. }
Output:
Modulus Assignment Operator (%=)
The modulus assignment operator (%=) computes the remainder of a
variable divided by a value and then assigns the resulting value to the
same variable. It takes the value on the right side of the operator, divides
it by the current value of the variable on the left side, and then assigns
the new value to the variable on the left side.
Example
1. public class Main {
2. public static void main(String args[]) {
3. int x = 10;
4. x %= 3;
5. [Link](x);
6. }
7. }
Output:
Bitwise Assignment Operators
We can also use the assignment operator with the bitwise operator. The
following table depicts an example of the bitwise assignment operator.
Operator Purpose Example Equival
a<<=b Left Shift Bits a<<=b a=a<<b
a>>=b Right Shift Bits a>>=b a=a>>b
a>>>=b Right Sih if Bits Zero a>>>=b a=a>>>
Fill
a&=b Bitwise AND a&=b a=a&b
a^=b Bitwise XOR a^=b a=a^b
a|=b Bitwise OR a|=b a=a|b
Example
1. public class Main {
2. public static void main(String[] args) {
3. int x = 10;
4. // Left shift assignment operator
5. x <<= 2;
6. [Link]("x <<= 2: " + x);
7. // Right shift assignment operator
8. x >>= 1;
9. [Link]("x >>= 1: " + x);
10. // Bitwise AND assignment operator
11. x &= 5;
12. [Link]("x &= 5: " + x);
13. // Bitwise OR assignment operator
14. x |= 2;
15. [Link]("x |= 2: " + x);
16. // Bitwise XOR assignment operator
17. x ^= 1;
18. [Link]("x ^= 1: " + x);
19. }
20. }
Output:
x <<= 2: 40
x >>= 1: 20
x &= 5: 4
x |= 2: 6
x ^= 1: 7
s