Assignment Operators
Let's take a look at the assignment operator and a few related shorthands.
We'll cover the following...
Introduction
We have already seen the assignment operator, which is used to assign a value to a variable or update its value.
The “equal to” sign(
Therefore, you always need to place a variable on the left side of =, whereas on the right side can be a value or another variable. Another thing you need to be careful about is the type. You can assign a double value or another double type variable only to a double type variable.
Let’s look at some use cases of the assignment operator.
class AssignmentOperator{public static void main(String args[]){// assignment operator used to assign a value of zero to the newly declared variableint var1 = 0;System.out.println(var1); // prints the value of var1, i.e., 0int var2; // another int type variable declared// assignment operator used to assign a value to a previously declared variablevar2 = 7;System.out.println(var2); // prints value of var2, i.e., 7// assignment operator used to update the value of var1 to the value of var2var1 = var2;System.out.println(var1); // prints updated value of var1 i.e. 7System.out.println(var2); // value of var2 remains 7}}
Notice that we create an int variable at line 6 and assign it with . Next, we declare another int variable: var2 (at line 9). At line 12, we give var2 a value of . Look at line 16.The statement, var1 = var2, changes the value of var1. After this statement, var1 equals .
You can also use the assignment operator along with arithmetic operators to update the values of variables. For example, if you want to add to the variable, var1, you could do that as follows:
class AssignmentOperatorArithmetic{public static void main(String args[]){// var1 declared and assigned a value of 7int var1 = 7;System.out.println(var1); // prints 7// adding 8 to var1var1 = var1 + 8;System.out.println(var1); // prints 15}}
At line 10, we are adding to the old value of var1, and storing the result back to var1. This statement makes var1's value equal .
Compound assignment operators
In the example above, when you add to var1, var1 is repeated twice: once on the left side of =, and once on the right. The same will be the case for any other arithmetic operator, i.e., -, *, / and %.
Well, this is not ideal. Writing a variable’s name twice like this can be infuriating. Thus, we have compound assignment operators. By prepending the arithmetic operator to = operator, we get the corresponding arithmetic operator. Thus, we have:
-
+=: Adds the value given on the right side (or the value of a variable given on the right side) to the variable on the left. -
-=: Subtracts the value given on the right side (or the value of a variable given on the right side) from the variable on the left. -
*=: Multiplies the value given on the right side (or the value of a variable given on the right side) with the variable on the left. -
/=: Divides the variable on the left with the value (or value of a variable) given to the right. -
%=: Takes the module of the variable on the left with the value (or value of a variable) given to the right.
Let’s look at all of these operators in practice in the code below.