Assignment Operators in Java

Assignment-Operators-in-Java-Feature.jpg

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 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

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 also 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:

Java

Output: 

1. Simple Assignment Operator

In the above Java program, the assignment operator is used for different purposes to show its functionality.

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 improves performance by reducing the overhead associated with variable reassignment in long expressions.

Here are the common compound assignment operators in Java.

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:

Java

Output: 

1. Addition Assignment Operator

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:
Java

Output: 

2. Subtraction Assignment Operator

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:

Java

Output: 

3. Multiplication Assignment Operator

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:

Java

Output: 

4. Division Assignment Operator

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:

Java

Output: 

5. Modulus Assignment Operator

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:

Java

Output:

6. Bitwise AND and Assign

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:

Java

Output:

7. Bitwise OR and Assign

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:

Java

Output:

8. Bitwise XOR and Assign

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:

Java

Output:

9. Left Shift and Assign

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:

Java

Output:

10. Right Shift and Assign

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:

Java

Output:

11. Unsigned Right Shift and Assign

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:

Java

Output: 

Chained Assignment Operators

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:

Java

Output: 

How Compound Assignment Operators Handle Type Casting in Java

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 :

Java

Output: 

Fixed Code by using the compound assignment operator

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

Java Assignment Operators are important and useful as they make your code efficient and faster. To leverage their power even more, we have listed down some of the best practices you should keep in mind when using them in your code.

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 makes 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 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 ==, which 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 to 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
quiz-icon

Useful Resources:

Conclusion

Assignment operators in Java are used to assign values to 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.

Prepare smarter for your next Java interview with our curated list of Java interview questions and answers.

Assignment Operators in Java – FAQs

Q1. What is the purpose of assignment operators in Java?

Assignment operators in Java are used to assign values to variables. The simplest is =, while compound operators like += or -= combine assignment with arithmetic operations.

Q2. Which cannot be used as an assignment operator?

Relational operators like ==, !=, <, and > cannot be used as assignment operators in Java since they evaluate conditions instead of assigning values.

Q3. Is the assignment operator binary or unary?

The assignment operator in Java is a binary operator because it requires two operands: a variable on the left and a value or expression on the right.

Q4. What is a compound assignment in Java?

A compound assignment operator in Java combines arithmetic with assignment. For example, x += 5 adds 5 to x and reassigns the result, instead of writing x = x + 5.

Q5. How does an assignment work in Java?

Assignment operators in Java first evaluate the right-hand expression and then store the result into the left-hand variable. This makes them essential for updating variable values efficiently.

Q6. How many assignment operators are there in Java?

There are 6 main assignment operators in Java: =, +=, -=, *=, /=, and %=.

Q7. What is the difference between simple and compound assignment operators in Java?

The simple operator = assigns values, while compound assignment operators like += or -= perform arithmetic and assign the result in one step.

Q8. Can we chain assignment operators in Java?

Yes, assignment operators in Java allow chaining. Example: int a, b, c; a = b = c = 10; assigns 10 to all three variables.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner