When you write Java code, one of the most common operations you will do is assigning values to variables. Assignment operators are used to assign values to variables. The most common one is the = operator. But Java has more assignment operators that help you write shorter and cleaner code.
In this guide, you will learn about all the assignment operators in Java with simple examples. Whether you are a beginner or just need a quick refresher, this post will help you understand them easily.
Table of Contents:
What are Assignment Operators in Java?
The assignment operators in Java are the operators used to assign values to the variables.
Syntax:
operand ASSIGNMENT_OPERATOR value;
The operand present at the left-hand side of the assignment operator is the variable in which the value will be stored. The right side of the operator is the value to be assigned to the variable.
Note: Kindly take care of the data types used while defining the value. The value of the variable should match the data type of the variable used.
Types of Assignment Operators in Java
There are 2 categories of assignment operators in Java. These are as follows:
1. Simple Assignment Operator
The simple assignment operator is used when there is a need only to assign the value. In this type of operator, no calculation is performed; they simply assign the value to the variable. The “=” operator is used to assign the value. The left side of the assignment operator is the variable, and the right side is the value to be assigned to the variable.
The assignment operator is not just used to assign the value to the variable, it can be used to assign,
- To primitive types
- With reference types
- Assigning one variable to another
Note: The value of the right side must be of the same data type that has been defined on the left side.
Syntax:
operand = value;
Example:
Output:
In the above Java program, the assignment operator is used for different purposes to show its functionality.
Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
2. Compound Assignment Operators
The compound assignment operators are the operators that assign the value to the variable as well as perform some type of calculation. The compound operators use “=” operator along with the “+, -, /, *” operators.
Using compound assignment operators not only saves lines of code, but also they improve the performance by reducing the overhead associated with variable reassignment in long expressions.
Here are the common compound assignment operators in Java.
Operator |
Description |
Syntax |
Equivalent To |
+= |
Add and assign |
a += b |
a = a + b |
-= |
Subtract and assign |
a -= b |
a = a – b |
*= |
Multiply and assign |
a *= b |
a = a * b |
/= |
Divide and assign |
a /= b |
a = a / b |
%= |
Modulus and assign |
a %= b |
a = a % b |
&= |
Bitwise AND and assign |
a &= b |
a = a & b |
|= |
Bitwise OR and assign |
a |= b |
a = a | b |
^= |
Bitwise XOR and assign |
a ^= b |
a = a ^ b |
<<= |
Left shift and assign |
a <<= b |
a = a << b |
>>= |
Right shift and assign (sign-propagating) |
a >>= b |
a = a >> b |
>>>= |
Unsigned right shift and assign |
a >>>= b |
a = a >>> b |
Let us discuss all the compound assignment operators in detail.
1. Addition Assignment Operator (+=)
This operator is the shorthand for the addition operator. It takes the value of operand1 and adds it to operand2, then assigns the result to operand1.
Syntax:
operand1 += operand2 ;
Example:
Output:
2. Subtraction Assignment Operator (-=)
This operator is the shorthand for the subtraction operator. It takes the value of operand2 and subtracts it from operand1.
Syntax:
operand1 -= operand2 ;
Example:
Output:
3. Multiplication Assignment Operator (*=)
This operator is the shorthand for the multiplication operator. It takes the value of operand1 and multiplies it by operand2, then assigns the result to operand1.
Syntax:
operand1 *= operand2 ;
Example:
Output:
4. Division Assignment Operator (/=)
This operator is the shorthand for the division operator. It takes the value of operand1 and divides it by operand2, then assigns the result to operand1.
Syntax:
operand1 /= operand2 ;
Example:
Output:
5. Modulus Assignment Operator (%=)
The modulus assignment operator divides the value of the operand1 by the operand2 and assigns the remainder back to the operand1.
Syntax:
operand1 %= operand2 ;
Example:
Output:
6. Bitwise AND and Assign (&=)
This operator performs a bitwise AND operation between operand1 and operand2, and assigns the result to operand1.
Syntax:
operand1 &= operand2;
Example:
Output:
7. Bitwise OR and Assign (|=)
This operator performs a bitwise OR between operand1 and operand2, and assigns the result to operand1.
Syntax:
operand1 |= operand2;
Example:
Output:
8. Bitwise XOR and Assign (^=)
This operator performs a bitwise XOR between operand1 and operand2, and assigns the result to operand1.
Syntax:
operand1 ^= operand2;
Example:
Output:
Get 100% Hike!
Master Most in Demand Skills Now!
9. Left Shift and Assign (<<=)
This operator shifts the bits of operand1 to the left by the number of positions specified by operand2, and assigns the result to operand1.
Syntax:
operand1 <<= operand2;
Example:
Output:
10. Right Shift and Assign (Sign-Propagating) (>>=)
This operator shifts the bits of operand1 to the right by the number of positions specified by operand2 and assigns the result to operand1. The sign bit is preserved (arithmetic shift).
Syntax:
operand1 >>= operand2;
Example:
Output:
11. Unsigned Right Shift and Assign (>>>=)
This operator shifts the bits of operand1 to the right by the number of positions specified by operand2, and assigns the result to operand1. It fills zeros from the left (logical shift).
Syntax:
operand1 >>>= operand2;
Example:
Output:
Chained Assignment Operators
Chained assignment operators allow you to assign the same value to multiple variables in a single statement.
Syntax:
operand1 = operand2 = operand3 =..........operandN=value;
Example:
Output:
In the above Java code, the value 10 is assigned to the variables a, b, and c through the chained assignment operators. Then the final results are printed.
How Compound Assignment Operators Handle Type Casting in Java
In Java, when you use compound operators like += or -=, Java automatically converts the result to match the type of the variable on the left side, so you don’t need to add a manual type cast.
The type casting occurs because Java defines the compound assignment operators as a special syntax that has built-in type conversion.
Example:
Output:
In the above Java code, the variable a is of the type byte, and the value 5 is added to it, without type casting it. Due to which the compilation of the program failed.
Now let us see the fixed code of the above code by using the compound assignment operator in Java.
Fixed Code by using the compound assignment operator :
Output:
In the above Java code, the value of a is of the type byte. When the value 5 is added to it using the compound assignment operator, the variable a is automatically typecast. Hence, no error occurs.
Best Practices For Assignment Operators in Java
1. Use Compound Assignment Operators for Shorter Code
You should use compound assignment operators in Java wherever it is necessary, as it saves space and make the code cleaner.
For example,
Instead of using
a = a + 5,
You can use
a += 5;.
2. Make Sure the Data Types Match
When using the compound operators in Java, make sure that the variables you are using have the same data type. You can assign a smaller type (like int) to a bigger one (like long) easily, but the vice versa is not always possible.
For example,
int a = 10;
long b = a; // works fine
int c = b; // this will cause an error, you need to cast it
3. Keep Your Code Readable
Do not use the assignment operators in such a way that the readability of the code changes and it becomes hard for the user to read.
For example
// Complex to read:
if ((x = getValue()) > 0) { ... }
// Easier to read:
x = getValue();
if (x > 0) { ... }
4. Don’t Mix Up = and ==
Properly use the assignment operators, = and ==, should be used carefully. = is used to assign the value, and == is used to compare the values of the variables. Using these operators with the if statement will result be a mistake.
For example,
int a = 5;
if (a = 5) { } // assignment, not comparison
if (a == 5) { } //comparison
5. Know How Operator Precedence Works
Some operators run before the others. For example, * is calculated before +, and + is calculated before =.
For example,
int a = 5;
a += 10 * 2; // a = a + (10 * 2) -> a = 5 + 20 = 25
6. Use the final Keyword
If You Don’t want to reassign the value, you can use the final keyword. It prevents changes of the value by mistake and also takes care of the important values.
Syntax:
final int MAX = 100;
Unlock Your Future in Java
Start Your Java Journey for Free Today
Conclusion
Assignment operators in Java are used to assign values to the variables, and in some cases, they are also used to perform some operations. The simple assignment operator (=) is used for direct value assignment, while compound assignment operators like +=, -=, and *= are used to perform the calculations and then assign the value to the operator. The compound assignment operators also perform type casting on the variables.
Assignment Operators in Java – FAQs
Q1. What is the purpose of assignment operators in Java?
The Assignment operator symbol is used for assigning values to the variables, with the most basic one being the equal sign (=).
Q2. Which can not be used as an assignment operator?
The == operator cannot be used as an assignment operator, as it is a comparison operator that is used to check equality, not to assign values.
Q3. Is the assignment operator binary or unary?
The binary assignment operators are the simple-assignment operator ( = ) and the compound-assignment operators. Each compound-assignment operator is a combination of another binary operator with the simple-assignment operator.
Q4. What is a compound assignment in Java?
Compound assignment operators are shortcuts that do a math operation and assignment in one step.
For example, x += 1 adds 1 to x and assigns the sum to x.
Q5. How does an assignment work in Java?
An assignment statement always has a single variable on the left-hand side of the = sign. The value of the expression on the right-hand side of the = sign is copied into the memory location of the variable on the left-hand side.