Introduction
Assignment operators in Java are used to assign values to variables. The most basic assignment operator is =
, which assigns the value on the right-hand side to the variable on the left-hand side. Java also provides a range of compound assignment operators that combine arithmetic operations with assignment, making it easier to write concise and efficient code.
Key Points:
- Basic Assignment: The
=
operator assigns the right-hand value to the left-hand variable. - Compound Assignment: Combines arithmetic operations with assignment for concise code.
- Multiple Uses: Commonly used for updating variables and performing arithmetic operations.
List of Assignment Operators
Java provides the following assignment operators:
Operator | Description | Example | Equivalent To |
---|---|---|---|
= | Simple assignment | a = b | a = b |
+= | Addition assignment | a += b | a = a + b |
-= | Subtraction assignment | a -= b | a = a - b |
*= | Multiplication assignment | a *= b | a = a * b |
/= | Division assignment | a /= b | a = a / b |
%= | Modulus assignment | a %= b | a = a % b |
&= | Bitwise AND assignment | a &= b | a = a & b |
|= | Bitwise OR assignment | a |= b | a = a | b |
^= | Bitwise XOR assignment | a ^= b | a = a ^ b |
<<= | Left shift assignment | a <<= b | a = a << b |
>>= | Right shift assignment | a >>= b | a = a >> b |
>>>= | Unsigned right shift assignment | a >>>= b | a = a >>> b |
Detailed Examples of Assignment Operators
Let’s explore each assignment operator with examples.
1. Simple Assignment (=)
The =
operator assigns the value on the right-hand side to the variable on the left-hand side.
public class SimpleAssignmentExample { public static void main(String[] args) { int a = 5; int b = 10; a = b; // Assign the value of b to a System.out.println("a: " + a); // Output: a: 10 } }
Explanation:
- Assignment: The value of
b
(which is 10) is assigned toa
.
Output:
a: 10
2. Addition Assignment (+=)
The +=
operator adds the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
public class AdditionAssignmentExample { public static void main(String[] args) { int a = 5; a += 3; // Equivalent to a = a + 3 System.out.println("a: " + a); // Output: a: 8 } }
Explanation:
- Addition Assignment: The value
3
is added toa
, and the result is stored back ina
.
Output:
a: 8
3. Subtraction Assignment (-=)
The -=
operator subtracts the right-hand operand from the left-hand operand and assigns the result to the left-hand operand.
public class SubtractionAssignmentExample { public static void main(String[] args) { int a = 5; a -= 2; // Equivalent to a = a - 2 System.out.println("a: " + a); // Output: a: 3 } }
Explanation:
- Subtraction Assignment: The value
2
is subtracted froma
, and the result is stored back ina
.
Output:
a: 3
4. Multiplication Assignment (*=)
The *=
operator multiplies the left-hand operand by the right-hand operand and assigns the result to the left-hand operand.
public class MultiplicationAssignmentExample { public static void main(String[] args) { int a = 5; a *= 4; // Equivalent to a = a * 4 System.out.println("a: " + a); // Output: a: 20 } }
Explanation:
- Multiplication Assignment: The value
4
is multiplied bya
, and the result is stored back ina
.
Output:
a: 20
5. Division Assignment (/=)
The /=
operator divides the left-hand operand by the right-hand operand and assigns the result to the left-hand operand.
public class DivisionAssignmentExample { public static void main(String[] args) { int a = 20; a /= 4; // Equivalent to a = a / 4 System.out.println("a: " + a); // Output: a: 5 } }
Explanation:
- Division Assignment: The value of
a
is divided by4
, and the result is stored back ina
.
Output:
a: 5
6. Modulus Assignment (%=)
The %=
operator calculates the modulus of the left-hand operand with the right-hand operand and assigns the result to the left-hand operand.
public class ModulusAssignmentExample { public static void main(String[] args) { int a = 10; a %= 3; // Equivalent to a = a % 3 System.out.println("a: " + a); // Output: a: 1 } }
Explanation:
- Modulus Assignment: The remainder of
a
divided by3
is stored back ina
.
Output:
a: 1
Conclusion
Java’s assignment operators are crucial for manipulating variables and performing arithmetic operations in a concise and efficient manner. By using compound assignment operators, developers can write cleaner and more maintainable code.
Summary:
- Simple Assignment (
=
): Assigns the right-hand value to the left-hand variable. - Addition Assignment (
+=
): Adds the right-hand value to the left-hand variable. - Subtraction Assignment (
-=
): Subtracts the right-hand value from the left-hand variable. - Multiplication Assignment (
*=
): Multiplies the left-hand variable by the right-hand value. - Division Assignment (
/=
): Divides the left-hand variable by the right-hand value. - Modulus Assignment (
%=
): Computes the remainder of dividing the left-hand variable by the right-hand value.