Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculation and functions be it logical, arithmetic, relational etc. They are classified based on the functionality they provide. Here are a few types:
- Arithmetic Operators
- Unary Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Ternary Operator
- Bitwise Operators
- Shift Operators
This article explains all that one needs to know regarding the Assignment Operators.
Assignment Operators
These operators are used to assign values to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the operand on the left side otherwise the compiler will raise an error. This means that the assignment operators have right to left associativity, i.e value given on the right-hand side of the operator is assigned to the variable on the left and therefore right-hand side value must be declared before using it or should be a constant. The general format of assignment operator is,
variable operator value;
Let’s look at each of the assignment operators and how they operate:
- “=”: This is the simplest assignment operator which is used to assign the value on the right to the variable on the left. This is the basic definition of assignment operator and how does it functions.
Syntax:num1 = num2;
Example:
a = 10; ch = 'y';
// Java code to illustrate "="importjava.io.*;classAssignment {publicstaticvoidmain(String[] args){// Declaring variablesintnum;String name;// Assigning valuesnum =10;name ="GeeksforGeeks";// Displaying the assigned valuesSystem.out.println("num is assigned: "+ num);System.out.println("name is assigned: "+ name);}}chevron_rightfilter_noneOutput:num is assigned: 10 name is assigned: GeeksforGeeks
- “+=”: This operator is a compound of ‘+’ and ‘=’ operators. It operates by adding the current value of the variable on left to the value on the right and then assigning the result to the operand on the left.
Syntax:num1 += num2;
Example:
a += 10 This means, a = a + 10
// Java code to illustrate "+="importjava.io.*;classAssignment {publicstaticvoidmain(String[] args){// Declaring variablesintnum1 =10, num2 =20;System.out.println("num1 = "+ num1);System.out.println("num2 = "+ num2);// Adding & Assigning valuesnum1 += num2;// Displaying the assigned valuesSystem.out.println("num1 = "+ num1);}}chevron_rightfilter_noneOutput:num1 = 10 num2 = 20 num1 = 30
- “-=”: This operator is a compound of ‘-‘ and ‘=’ operators. It operates by subtracting the value of the variable on right from the current value of the variable on the left and then assigning the result to the operand on the left.
Syntax:
num1 -= num2;
Example:
a -= 10 This means, a = a - 10
// Java code to illustrate "-="importjava.io.*;classAssignment {publicstaticvoidmain(String[] args){// Declaring variablesintnum1 =10, num2 =20;System.out.println("num1 = "+ num1);System.out.println("num2 = "+ num2);// Subtracting & Assigning valuesnum1 -= num2;// Displaying the assigned valuesSystem.out.println("num1 = "+ num1);}}chevron_rightfilter_noneOutput:num1 = 10 num2 = 20 num1 = -10
- “*=”: This operator is a compound of ‘*’ and ‘=’ operators. It operates by multiplying the current value of the variable on left to the value on the right and then assigning the result to the operand on the left.
Syntax:num1 *= num2;
Example:
a *= 10 This means, a = a * 10
// Java code to illustrate "*="importjava.io.*;classAssignment {publicstaticvoidmain(String[] args){// Declaring variablesintnum1 =10, num2 =20;System.out.println("num1 = "+ num1);System.out.println("num2 = "+ num2);// Multiplying & Assigning valuesnum1 *= num2;// Displaying the assigned valuesSystem.out.println("num1 = "+ num1);}}chevron_rightfilter_noneOutput:num1 = 10 num2 = 20 num1 = 200
- “/=”: This operator is a compound of ‘/’ and ‘=’ operators. It operates by dividing the current value of the variable on left by the value on the right and then assigning the quotient to the operand on the left.
Syntax:num1 /= num2;
Example:
a /= 10 This means, a = a / 10
// Java code to illustrate "/="importjava.io.*;classAssignment {publicstaticvoidmain(String[] args){// Declaring variablesintnum1 =20, num2 =10;System.out.println("num1 = "+ num1);System.out.println("num2 = "+ num2);// Dividing & Assigning valuesnum1 /= num2;// Displaying the assigned valuesSystem.out.println("num1 = "+ num1);}}chevron_rightfilter_noneOutput:num1 = 20 num2 = 10 num1 = 2
- “%=”: This operator is a compound of ‘%’ and ‘=’ operators. It operates by dividing the current value of the variable on left by the value on the right and then assigning the remainder to the operand on the left.
Syntax:num1 %= num2;
Example:
a %= 3 This means, a = a % 3
// Java code to illustrate "%="importjava.io.*;classAssignment {publicstaticvoidmain(String[] args){// Declaring variablesintnum1 =5, num2 =3;System.out.println("num1 = "+ num1);System.out.println("num2 = "+ num2);// Moduling & Assigning valuesnum1 %= num2;// Displaying the assigned valuesSystem.out.println("num1 = "+ num1);}}chevron_rightfilter_noneOutput:num1 = 5 num2 = 3 num1 = 2
Note: In many cases, the assignment operators can be combined with other operators to build a shorter version of the statement called Compound Statement. For example, instead of a = a+5, we can write a += 5. Let’s look at similar assignment operators that form the compound statements.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Compound assignment operators in Java
- Interesting facts about Array assignment in Java
- Difference between Simple and Compound Assignment in Java
- && operator in Java with Examples
- & Operator in Java with Examples
- Equality (==) operator in Java with Examples
- Java Unary Operator with Examples
- Java Ternary Operator with Examples
- Diamond operator for Anonymous Inner Class with Examples in Java
- new operator vs newInstance() method in Java
- instanceof operator vs isInstance() method in Java
- new operator in Java
- Java Ternary Operator Puzzle
- Double colon (::) operator in Java
- Difference between concat() and + operator in Java
- || operator in Java
- Multiples of 3 and 5 without using % operator
- How can we use Comma operator in place of curly braces?
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.BitSet class in Java with Examples | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

