JavaOperators
Assignment Operators
1. Introduction
Assignment operators in Java are used to assign values to variables.
The most commonly used assignment operator is =, but Java provides several compound assignment operators that simplify expressions and make code cleaner.
Assignment operators help you:
- Update variable values efficiently
- Reduce repetitive code
- Perform arithmetic and assignment in one step
- Improve readability in loops and calculations
This document explains each assignment operator with examples.
2. Basic Assignment Operator (=)
This operator assigns the value on the right-hand side (RHS) to the left-hand side (LHS) variable.
Syntax:
variable = value;Example:
int x = 10;
String name = "John";3. Compound Assignment Operators
Compound operators perform an operation and assignment in a single step.
Example:
x = x + 5;Can be written as:
x += 5;List of Compound Assignment Operators
| Operator | Meaning | Example | Equivalent To | |||
|---|---|---|---|---|---|---|
+= | Add and assign | x += 5 | x = x + 5 | |||
-= | Subtract and assign | x -= 3 | x = x - 3 | |||
*= | Multiply and assign | x *= 2 | x = x * 2 | |||
/= | Divide and assign | x /= 4 | x = x / 4 | |||
%= | Modulo and assign | x %= 2 | x = x % 2 | |||
&= | Bitwise AND and assign | x &= 3 | x = x & 3 | |||
^= | Bitwise XOR and assign | x ^= 6 | x = x ^ 6 | |||
<<= | Left shift and assign | x <<= 1 | x = x << 1 | |||
>>= | Right shift and assign | x >>= 1 | x = x >> 1 | |||
>>>= | Unsigned right shift and assign | x >>>= 2 | x = x >>> 2 |

4. Detailed Examples
4.1 Addition Assignment (+=)
int x = 10;
x += 5; Now:
x = 154.2 Subtraction Assignment (-=)
int x = 20;
x -= 4;Now:
x = 164.3 Multiplication Assignment (*=)
int x = 7;
x *= 3;Now:
x = 214.4 Division Assignment (/=)
int x = 40;
x /= 5;Now:
x = 84.5 Modulus Assignment (%=)
int x = 13;
x %= 5;Now:
x = 35. Bitwise Assignment Operators
These work at the bit level.
Example:
int x = 6; // binary: 110
x &= 3; // binary: 011Now:
x = 2 // binary: 0106. Shift Assignment Operators
Used to shift bits to the left or right.
Left shift:
int x = 4; // 0100
x <<= 1; // becomes 1000Now:
x = 8Right shift:
int x = 16; // 10000
x >>= 2; // becomes 00010Now:
x = 4
7. Real-World Use Cases
Loop counters
i += 2; // increment by 2Summations
sum += value;Reducing repetitive arithmetic
total -= discount;
balance *= rate;Working with bitwise flags
permissions |= READ;
permissions &= ~WRITE;8. Common Mistakes
- Using
/=with integers (may cause integer division) - Forgetting type limitations with
+=on small types (e.g., byte/short) - Confusing
x += ywithx =+ y(which assigns positive value)
Bad:
x =+ 5; // means x = +5, not x += 59. Summary
- Assignment operators simplify updating variable values.
=is the basic operator; others combine arithmetic and assignment.- Compound operators help write cleaner and shorter code.
- Bitwise and shift assignment operators are useful for performance and low-level programming.
- Understanding assignment operators is essential for loops, conditions, and calculations.
How is this guide?
Last updated on
