Java Assignment Operators
Java Assignment Operators are used to optionally perform an action with given operands and assign the result back to given variable (left operand).
The syntax of any Assignment Operator with operands is
operand1 operator_symbol operand2
In this tutorial, we will learn about different Assignment Operators available in Java programming language and go through each of these Assignment Operations in detail, with the help of examples.
Operator Symbol – Example – Description
The following table specifies symbol, example, and description for each of the Assignment Operator in Java.
Assignment Operation | Operator Symbol | Example | Description |
---|---|---|---|
Simple Assignment | = | x = 2 | Assign x with 2. |
Addition Assignment | += | x += 3 | Add 3 to the value of x and assign the result to x. |
Subtraction Assignment | -= | x -= 3 | Subtract 3 from x and assign the result to x. |
Multiplication Assignment | *= | x *= 3 | Multiply x with 3 and assign the result to x. |
Division Assignment | /= | x /= 3 | Divide x with 3 and assign the quotient to x. |
Remainder Assignment | %= | x %= 3 | Divide x with 3 and assign the remainder to x. |
Bitwise AND Assignment | &= | x &= 3 | Perform x & 3 and assign the result to x. |
Bitwise OR Assignment | |= | x |= 3 | Perform x | 3 and assign the result to x. |
Bitwise-exclusive-OR Assignment | ^= | x ^= 3 | Perform x ^ 3 and assign the result to x. |
Left-shift Assignment | <<= | x <<= 3 | Left-shift the value of x by 3 places and assign the result to x. |
Right-shift Assignment | >>= | x >>= 3 | Right-shift the value of x by 3 places and assign the result to x. |
Simple Assignment
In the following example, we assign a value of 2 to x using Simple Assignment Operator.
Main.java
public class Main {
public static void main(String[] args) {
int x;
//simple assignment
x = 2;
System.out.println("x : " + x);
}
}
Output
x : 2
Addition Assignment
In the following example, we add 3 to x and assign the result to x using Addition Assignment Operator.
main.cpp
public class Main {
public static void main(String[] args) {
int x = 2;
//addition assignment
x += 3;
System.out.println("x : " + x);
}
}
Output
x : 5
Subtraction Assignment
In the following example, we subtract 3 from x and assign the result to x using Subtraction Assignment Operator.
main.cpp
public class Main {
public static void main(String[] args) {
int x = 2;
//subtraction assignment
x -= 3;
System.out.println("x : " + x);
}
}
Output
x : -1
Multiplication Assignment
In the following example, we multiply 3 to x and assign the result to x using Multiplication Assignment Operator.
main.cpp
public class Main {
public static void main(String[] args) {
int x = 2;
//multiplication assignment
x *= 3;
System.out.println("x : " + x);
}
}
Output
x : 6
Division Assignment
In the following example, we divide x by 3 and assign the quotient to x using Division Assignment Operator.
main.cpp
public class Main {
public static void main(String[] args) {
int x = 7;
//division assignment
x /= 3;
System.out.println("x : " + x);
}
}
Output
x : 2
Remainder Assignment
In the following example, we divide x by 3 and assign the remainder to x using Remainder Assignment Operator.
main.cpp
public class Main {
public static void main(String[] args) {
int x = 7;
//remainder assignment
x %= 3;
System.out.println("x : " + x);
}
}
Output
x : 1
Bitwise AND Assignment
In the following example, we do bitwise AND operation between x and 3 and assign the result to x using Bitwise AND Assignment Operator.
main.cpp
public class Main {
public static void main(String[] args) {
int x = 10;
//bitwise AND assignment
x &= 3;
System.out.println("x : " + x);
}
}
Output
x : 2
Bitwise OR Assignment
In the following example, we do bitwise OR operation between x and 3 and assign the result to x using Bitwise OR Assignment Operator.
main.cpp
public class Main {
public static void main(String[] args) {
int x = 10;
//bitwise OR assignment
x |= 3;
System.out.println("x : " + x);
}
}
Output
x : 11
Bitwise XOR Assignment
In the following example, we do bitwise XOR operation between x and 3 and assign the result to x using Bitwise XOR Assignment Operator.
main.cpp
public class Main {
public static void main(String[] args) {
int x = 10;
//bitwise XOR assignment
x ^= 3;
System.out.println("x : " + x);
}
}
Output
x : 9
Left-shift Assignment
In the following example, we left-shift x by 3 places and assign the result to x using Left-shift Assignment Operator.
main.cpp
public class Main {
public static void main(String[] args) {
int x = 10;
//left shift assignment
x <<= 3;
System.out.println("x : " + x);
}
}
Output
x : 80
Right-shift Assignment
In the following example, we right-shift x by 3 places and assign the result to x using Right-shift Assignment Operator.
main.cpp
public class Main {
public static void main(String[] args) {
int x = 10;
//right shift assignment
x >>= 3;
System.out.println("x : " + x);
}
}
Output
x : 1
Conclusion
In this Java Tutorial, we learned what Assignment Operators are, and how to use them in Java programs, with the help of examples.