Assignment operators in Java are used to assign values to variables and simplify expressions. They include both simple (=) and compound operators (like +=, -=), which combine operations with assignment. These operators help write cleaner and more concise code while handling value updates efficiently.
- Supports both simple (
=) and compound (+=,-=,*=,/=,%=) assignments. - Compound operators perform implicit type casting in some cases.
- Reduces code length by combining operation and assignment in one step.
Types of Assignment Operators in Java
The Assignment Operator is generally of two types. They are:
1. Simple Assignment Operator
The Simple Assignment Operator is used with the “=” sign where the left side consists of the operand and the right side consists of a value. The value of the right side must be of the same data type that has been defined on the left side.
2. Compound Assignment Operator
The Compound Operator is used where +,-,*, and / is used along with the = operator.
Below is an explanation of each assignment operator and its working
1. (=) operator
This is the most straightforward assignment operator, which is used to assign the value on the right to the variable on the left. This is the basic definition of an assignment operator and how it functions.
Syntax:
num1 = num2;
import java.io.*;
class Assignment {
public static void main(String[] args)
{
// Declaring variables
int num;
String name;
// Assigning values
num = 10;
name = "GeeksforGeeks";
// Displaying the assigned values
System.out.println("num is assigned: " + num);
System.out.println("name is assigned: " + name);
}
}
Output
num is assigned: 10 name is assigned: GeeksforGeeks
2. (+=) operator
This operator is a compound of '+' and '=' operators. It operates by adding the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left.
Syntax:
num1 += num2;
import java.io.*;
class Assignment {
public static void main(String[] args)
{
// Declaring variables
int num1 = 10, num2 = 20;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// Adding & Assigning values
num1 += num2;
// Displaying the assigned values
System.out.println("num1 = " + num1);
}
}
Output
num1 = 10 num2 = 20 num1 = 30
Note: In Java, the compound assignment operator (+=) performs implicit type casting.
- x = x + 4.5; -> gives a compile-time error (double -> int conversion not allowed).
- x += 4.5; -> works because it automatically casts the result to int, giving output 9.
3. (-=) operator
This operator is a compound of '-' and '=' operators. It operates by subtracting the variable's value on the right from the current value of the variable on the left and then assigning the result to the operand on the left.
Syntax:
num1 -= num2;
import java.io.*;
class Assignment {
public static void main(String[] args)
{
// Declaring variables
int num1 = 10, num2 = 20;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// Subtracting & Assigning values
num1 -= num2;
// Displaying the assigned values
System.out.println("num1 = " + num1);
}
}
Output
num1 = 10 num2 = 20 num1 = -10
4. (*=) operator
This operator is a compound of '*' and '=' operators. It operates by multiplying the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left.
Syntax:
num1 *= num2;
import java.io.*;
class Assignment {
public static void main(String[] args)
{
// Declaring variables
int num1 = 10, num2 = 20;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// Multiplying & Assigning values
num1 *= num2;
// Displaying the assigned values
System.out.println("num1 = " + num1);
}
}
Output
num1 = 10 num2 = 20 num1 = 200
5. (/=) operator
This operator is a compound of '/' and '=' operators. It operates by dividing the current value of the variable on the left by the value on the right and then assigning the quotient to the operand on the left.
Syntax:
num1 /= num2;
import java.io.*;
class Assignment {
public static void main(String[] args)
{
// Declaring variables
int num1 = 20, num2 = 10;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// Dividing & Assigning values
num1 /= num2;
// Displaying the assigned values
System.out.println("num1 = " + num1);
}
}
Output
num1 = 20 num2 = 10 num1 = 2
6. (%=) operator
This operator is a compound of '%' and '=' operators. It operates by dividing the current value of the variable on the left by the value on the right and then assigning the remainder to the operand on the left.
Syntax:
num1 %= num2;
import java.io.*;
class Assignment {
public static void main(String[] args)
{
// Declaring variables
int num1 = 5, num2 = 3;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// Moduling & Assigning values
num1 %= num2;
// Displaying the assigned values
System.out.println("num1 = " + num1);
}
}
Output
num1 = 5 num2 = 3 num1 = 2
7. (&=) Bitwise AND and Assign
Performs bitwise AND operation and assigns the result. Works at binary level on bits of numbers.
Syntax:
variable &= value;
public class Assignment{
public static void main(String[] args) {
int a = 5; // 0101
a &= 3; // 0011
System.out.println(a);
}
}
Output
1
8. (|=) Bitwise OR and Assign
Performs bitwise OR operation and assigns the result. Sets bits where either operand has 1.
Syntax:
variable |= value;
public class Assignment{
public static void main(String[] args) {
int a = 5; // 0101
a |= 3; // 0011
System.out.println(a);
}
}
Output
7
9. (^=) Bitwise XOR and Assign
Performs bitwise XOR and assigns the result. Sets bit to 1 only when bits are different.
public class Assignment{
public static void main(String[] args) {
int a = 5; // 0101
a ^= 3; // 0011
System.out.println(a);
}
}
10. (<<=) Left Shift and Assign
Shifts bits to the left and assigns the result. Equivalent to multiplying by powers of 2.
Syntax:
variable <<= value;
public class Assignment{
public static void main(String[] args) {
int a = 5; // 0101
a <<= 1; // shift left by 1
System.out.println(a);
}
}
Output
10
11. (>>=) Right Shift and Assign
Shifts bits to the right and assigns the result. Equivalent to dividing by powers of 2.
Syntax:
variable >>= value;
public class Assignment{
public static void main(String[] args) {
int a = 8; // 1000
a >>= 1; // shift right by 1
System.out.println(a);
}
}
Output
4