Java Assignment Operators
Assignment operators in Java are used to assign values to variables. The basic assignment operator is =
, but there are several compound assignment operators that combine an arithmetic operation with assignment. Here’s a detailed explanation of assignment operators in Java.
Basic Assignment Operator (=
)
The basic assignment operator is used to assign the value of the right operand to the left operand.
Example:
public class BasicAssignmentExample {
public static void main(String[] args) {
int a = 10; // Assigns 10 to variable a
System.out.println("Value of a: " + a); // Output: Value of a: 10
}
}
Compound Assignment Operators
Compound assignment operators are shorthand for performing an operation on a variable and then assigning the result back to that variable. They combine an arithmetic operation with the assignment operator.
1. Addition Assignment Operator (+=
)
Adds the right operand to the left operand and assigns the result to the left operand.
Example:
public class AdditionAssignmentExample {
public static void main(String[] args) {
int a = 10;
a += 5; // Equivalent to a = a + 5
System.out.println("Value of a after += 5: " + a); // Output: Value of a after += 5: 15
}
}
2. Subtraction Assignment Operator (-=
)
Subtracts the right operand from the left operand and assigns the result to the left operand.
Example:
public class SubtractionAssignmentExample {
public static void main(String[] args) {
int a = 10;
a -= 3; // Equivalent to a = a - 3
System.out.println("Value of a after -= 3: " + a); // Output: Value of a after -= 3: 7
}
}
3. Multiplication Assignment Operator (*=
)
Multiplies the left operand by the right operand and assigns the result to the left operand.
Example:
public class MultiplicationAssignmentExample {
public static void main(String[] args) {
int a = 10;
a *= 2; // Equivalent to a = a * 2
System.out.println("Value of a after *= 2: " + a); // Output: Value of a after *= 2: 20
}
}
4. Division Assignment Operator (/=
)
Divides the left operand by the right operand and assigns the result to the left operand.
Example:
public class DivisionAssignmentExample {
public static void main(String[] args) {
int a = 20;
a /= 4; // Equivalent to a = a / 4
System.out.println("Value of a after /= 4: " + a); // Output: Value of a after /= 4: 5
}
}
5. Modulus Assignment Operator (%=
)
Calculates the remainder of the division of the left operand by the right operand and assigns the result to the left operand.
Example:
public class ModulusAssignmentExample {
public static void main(String[] args) {
int a = 10;
a %= 3; // Equivalent to a = a % 3
System.out.println("Value of a after %= 3: " + a); // Output: Value of a after %= 3: 1
}
}
Summary
Assignment operators in Java are essential for assigning values to variables efficiently. The basic assignment operator allows you to set a variable's value, while compound assignment operators provide a concise way to update a variable using arithmetic operations. Understanding these operators is fundamental to writing effective and efficient Java code.