Operator is a symbol that instructs the compiler to perform a specific action. For example, a “+” operator instructs the compiler to perform addition, a “>” operator instructs the compiler to perform comparison, “=” for assignment and so on. The operators in java are classified in eight different categories. In this guide, we will mainly discuss Assignment operators in Java.
In any operation, there is an operator and operands. For example: In a+b, the “+” symbol is the operator and a & b are operands.
Assignment Operator in Java
The following assignment operators are supported in Java.
The associativity of assignment operator is “right to left”, which means the when compiler encounters assignment operator, it starts to evaluate the expression from right to left.
For example:
int num = 10; The compiler assigns the value 10 to the variable num.
1. = Operator example
This is the simple assignment operator. The right side value is assigned to the operand on the left side.
public class JavaExample { public static void main(String[] args) { int n; //integer variable char ch; //character variable String str; //string variable // Simple assignment operator to assign values to variables n = 1; ch = 'A'; str = "BeginnersBook.com"; // Displaying the values of all the variables System.out.println("The value assigned to 'n': " + n); System.out.println("The value assigned to 'ch': " + ch); System.out.println("The value assigned to 'str': " + str); } }
Output:
2. += Operator example
This is a combination of + and = operators. a += b;
is equivalent to a = a+b;
This means the right side value is added to the left side value and the result is assigned to the left side operand.
public class JavaExample { public static void main(String[] args) { //integer variables int n1=1, n2=2, n3=3; //+= operator is used here to increase the value of variables //by the number mentioned in the right side of operator n1+=10; n2+=5; n3+=100; System.out.println(n1); System.out.println(n2); System.out.println(n3); } }
Output:
11 7 103
3. -= Operator example
This is a combination of – and = operators. a -= b;
is equivalent to a = a-b;
The value on the right side is subtracted from the left side variable’s value and the result is assigned back to the variable on left side.
public class JavaExample { public static void main(String[] args) { //integer variables int num1=100, num2=200, num3=300; //-= operator is used here to increase the value of variables //by the number mentioned in the right side of operator num1-=50; num2-=100; num3-=200; System.out.println(num1); System.out.println(num2); System.out.println(num3); } }
Output:
50 100 100
4. *= Operator example
It is a combination of * and = operators. The value on the right side is multiplied to the left side variable’s value and the result is assigned back to the left side variable. a *= b;
is equivalent to a = a*b;
.
public class JavaExample { public static void main(String[] args) { //declaring integer variables int num1=10, num2 =20; //Operator *= used here to multiply the num1 by num2 //and assign the result to num1 num1 *=num2; System.out.println(num1); System.out.println(num2); } }
Output:
200 20
5. /= Operator example
It is a combination of / and = operators. The compiler divides the left side variable’s value by the right side value and the quotient is assigned back to the left side variable. a /= b;
is equivalent to a = a/b;
.
public class JavaExample { public static void main(String[] args) { //declaring integer variables int x=100, y =20; //Operator /= used here to divide the variable x by // variable y and assign the quotient to x x /=y; System.out.println("Value of variable x: "+x); System.out.println("Value of variable y: "+y); } }
Output:
6. %= Operator example
It is a combination of % and = operators. The compiler divides the left side variable’s value by the right side value and the remainder is assigned back to the left side variable. a %= b;
is equivalent to a = a%b;
.
public class JavaExample { public static void main(String[] args) { //declaring integer variables int x=100, y =20; //Operator %= used here to divide the variable x by // variable y and assign the remainder to x //dividing 100 by 20 gives quotient value as 5 and // remainder value as 0 as it is perfectly divisible x %=y; System.out.println("Value of variable x: "+x); System.out.println("Value of variable y: "+y); } }
Output:
Assignment operator in For loop
Assignment operator is used in for loop as well.
class JavaExample { public static void main(String[] args) { int n = 10; // The first expression of the for loop is // an assignment operation where the value // of the loop variable is initialized for (int i = 1; i <= n; ++i) { System.out.print(i+ " "); } } }
Output:
1 2 3 4 5 6 7 8 9 10
Frequently asked Questions on Assignment operators:
Is it possible to do Assignment operator Overloading?
No, Java doesn’t support operator overloading. C++ allows operator overloading. This is covered in the C++ vs Java post.
What does an assignment return in Java?
An assignment operator return the value specified by the left operand after the assignment. The type of the return value is the type of the left operand. For Example:
public class JavaExample { public static void main(String[] args) { int num; if(10==(num=10)){ System.out.println("The value of num is: "+num); } } }
Output:
The value of num is: 10
As you can see, In the above example, we are using assignment operator in if statement. We did a comparison of value 10 to an assignment operator which resulted in a ‘true’ output because the return of assignment operator is the value of left operand.