Assignment Operator in Java
Variables are assigned values using these operators. The assignment operator's left operand is a variable, while the assignment operator's right operand is a value assigned to the variable. The right-side value must be of the same data type as the operand on the left. Right to left associativity exists for assignment operators, which means that the value supplied on the operator's right-hand side gets assigned to the variable on the left.
Syntax
var_name <operator> value;
Types of Assignment Operators in Java
We have the following types of assignment operators in java:
= Operator
This is the simple assignment operator. Values from the operand on the right side of the operator are assigned to the operand on the left side of the operator.
Syntax
var1 = var2;
+= Operator
This is a compound assignment operator. This operator comprises the '+' and '=' operators. Values from the operand on the right side of the operator are added to the value on the left side of the operand, and the result is assigned to the operand on the left side of the operator.
Syntax
var1 += var2;
Above statement is same as var1=var1+var2.
-= Operator
This is a compound assignment operator. This operator comprises the '-' and '=' operators. Values from the operand on the right side of the operator are subtracted from the value on the left side of the operand and the result is assigned to the operand on the left side of the operator.
Syntax
var1 -= var2;

You can also try this code with Online Java Compiler
Run CodeAbove statement is same as var1=var1-var2.
*= Operator
This is a compound assignment operator. This operator combines the '*' and '=' operators. Values from the operand on the right side of the operator are multiplied by the value on the left side of the operand, and the result is assigned to the operand on the left side of the operator.
Syntax
var1 *= var2;

You can also try this code with Online Java Compiler
Run CodeAbove statement is same as var1=var1*var2.
/= Operator
This is a compound assignment operator. This operator combines the '/' and the '=' operators. Values from the operand on the left side of the operator are divided by the value on the right side of the operand, and the result is assigned to the operand on the left side of the operator.
Syntax
var1 /= var2;

You can also try this code with Online Java Compiler
Run CodeAbove statement is same as var1=var1/var2.
%= Operator
This is a compound assignment operator. This operator combines the '%' and '=' operators. Values from the operand on the left side of the operator are divided by the value on the right side of the operand, and the remainder is assigned to the operand on the left side of the operator.
Syntax
var1 %= var2;

You can also try this code with Online Java Compiler
Run CodeThe above statement is the same as var1=var1%var2.
Example of Java Assignment Operators
import java.io.*;
public class Main
{
public static void main(String[] args) {
int num1=10,num2=5;
//= operator
num1=7;
System.out.println(num1);
//+= operator
num1=10;num2=5;
num1+=num2;
System.out.println(num1);
//-= operator
num1=10;num2=5;
num1-=num2;
System.out.println(num1);
//+= operator
num1=10;num2=5;
num1+=num2;
System.out.println(num1);
//*= operator
num1=10;num2=5;
num1*=num2;
System.out.println(num1);
///= operator
num1=10;num2=5;
num1/=num2;
System.out.println(num1);
//%= operator
num1=10;num2=5;
num1%=num2;
System.out.println(num1);
}
}

You can also try this code with Online Java Compiler
Run CodeOutput
7
15
5
15
50
2
0
Practice by yourself on online java compiler.
Frequently Asked Questions
What does += operator do?
This operator comprises the '+' and '=' operators. Values from the operand on the right side of the operator are added to the value on the left side of the operand, and the result is assigned to the operand on the left side of the operator.
Example var1 += var2;
What is the use of the modulus operator?
Modulus operator returns the remainder of division of two numbers. For example, 5%2 will return 1 since the remainder is 1 when 5 is divided by 2.
What are compound statement operators?
The compound Assignment operator is the operator that combines Arithmetic operator and assignment.
Conclusion
In this article, we have extensively discussed Assignment Operators and their implementation in Java. We tried to cover it with some examples.