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