Assignment Operators in Java
0 224
What are Assignment Operators in Java?
Assignment Operators in Java are used to assign values to variables. Beyond the simple assignment operator =
, Java provides a variety of compound assignment operators that combine an operation with assignment, making code shorter and easier to read.
Simple Assignment Operator (=)
The most basic assignment operator is =
. It assigns the value on its right to the variable on its left.
Example of Simple Assignment
int x = 10; // assigns the value 10 to variable x
System.out.println(x); // Output: 10
Compound Assignment Operators
Compound assignment operators combine a mathematical or bitwise operation with assignment. For example, +=
adds a value to a variable and assigns the result back to that variable.
This reduces repetition and enhances code clarity.
Common Compound Assignment Operators
+=
(Add and assign)-=
(Subtract and assign)*=
(Multiply and assign)/=
(Divide and assign)%=
(Modulus and assign)&=
(Bitwise AND and assign)|=
(Bitwise OR and assign)^=
(Bitwise XOR and assign)<<=
(Left shift and assign)>>=
(Right shift and assign)>>>=
(Unsigned right shift and assign)
Examples of Compound Assignment Operators
int a = 10;
a += 5; // equivalent to a = a + 5
System.out.println(a); // Output: 15
a -= 3; // equivalent to a = a - 3
System.out.println(a); // Output: 12
a *= 2; // equivalent to a = a * 2
System.out.println(a); // Output: 24
a /= 4; // equivalent to a = a / 4
System.out.println(a); // Output: 6
a %= 4; // equivalent to a = a % 4
System.out.println(a); // Output: 2
Bitwise Compound Assignment Operators
You can also combine bitwise operations with assignment using operators like &=
, |=
, and ^=
. These are especially useful when working with flags or binary data.
Example of Bitwise Assignment
int flags = 0b1010; // binary: 1010
flags &= 0b1100; // bitwise AND and assign (1010 & 1100 = 1000)
System.out.println(Integer.toBinaryString(flags)); // Output: 1000
flags |= 0b0011; // bitwise OR and assign (1000 | 0011 = 1011)
System.out.println(Integer.toBinaryString(flags)); // Output: 1011
flags ^= 0b0101; // bitwise XOR and assign (1011 ^ 0101 = 1110)
System.out.println(Integer.toBinaryString(flags)); // Output: 1110
Why Use Assignment Operators?
Compound assignment operators help keep your code concise and readable. They reduce redundancy by eliminating the need to repeat the variable name on both sides of the assignment, which also minimizes the chance of mistakes.
Summary
Assignment Operators in Java are fundamental tools for managing variables and performing operations efficiently.
Whether you’re simply assigning values or combining arithmetic and bitwise operations with assignment, mastering these operators will improve your coding fluency and style.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments