Assignment Operators in Java11 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 OperatorsThere are the following two types of assignment operators in Java.
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: The above statement assigns the right-side value to the left-hand side variable. Compound Assignment OperatorCompound 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.
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. ExampleCompile and RunOutput: 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. ExampleCompile and RunOutput: 5 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. ExampleCompile and RunOutput: 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. ExampleCompile and RunOutput: 2 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. ExampleCompile and RunOutput: 1 Bitwise Assignment OperatorsWe can also use the assignment operator with the bitwise operator. The following table depicts an example of the bitwise assignment operator.
ExampleCompile and RunOutput: x <<= 2: 40 x >>= 1: 20 x &= 5: 4 x |= 2: 6 x ^= 1: 7 Important Points to Remember
Java Assignment Operator MCQs1. Which of the following is NOT an assignment operator?
Answer: 2) Explanation: + is not an assignment operator because it does not have the assignment operator. 2. What does the operator >>=do?
Answer: 1) Explanation: The operator >>= right-shifts the value and assigns the result to a variable. 3. Which of the following is equivalent of a*=5?
Answer: 1) Explanation: The expression a=a*5 is equivalent to the expression a*=5 because both expression performs the same operation. 4. Which of the following operators is used for zero-fill right shift?
Answer: 4) Explanation: The operator >>>= is used to perform right-shifts, zero-filled operation. 5. Evaluate the value of x for the expression x+=6, when x is 4.
Answer: 3) Explanation: The expression x+=6 can be written as x=x+6. The value of x is 4, so 6+4 = 10., Hence, the value of x will be 10. Next TopicBitwise-operators-in-java |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India