Assignment Operators Java
Well, as the name suggests, the assignment operators are used for doing assignments. Not our college/school assignments, but assigning some values to some variable.
Java Assignment Operators
Here is a list of some assignment operators –
Assignment operator symbol | Description |
= | We have been using this operator unknowingly for a while. This is the simple assignment operator, only used to assign some value to some variable. We are going to have an example below to understand this one. |
+= | This operator is referred to as add and assign operator. All we are going to do here is add some specified value to the existing value of the variable, and assign the new value back to the variable. If you could not get the thing completely, refer to the below code, and then read this description again. |
-= | This operator is referred to as subtract and assign operator. We are going to subtract the specified value from the existing value and assign the new value back to the variable. Again if you are having difficulty understanding this, just refer to the below program and then read this description again. |
*= | This operator is referred to as the multiply and assign operator. We are going to just multiply the existing value from the variable with some specified value, and then assign the new value back to the variable. The same thing again, for any difficulty, just refer to the below code, and then read this description again. If the difficulty persists, we have some more descriptions following the program just to describe what’s happening. This applies to all other assignment operators described above and below. |
/= | This operator is referred to as the divide and assign operator. We are going to divide the existing value from the variable, with some specified number here, and assign the resulting value back to the variable. Refer to the code below to understand the thing in a better way, and then read this description again. |
%= | This operator is referred to as the modulo and assign operator. With this operator, we are going to calculate the remainder when we divide the value from the variable by some specified value and assign that remainder back to the variable. Refer to the below code to have a clear picture of what we are doing. |
So, now let’s solve a program that would help us understand the assignment operators –
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Understanding assignment operators…
int num1 = 5; // this is simple assignment
int num2 = 10;// just creating another variable because… why not?
num2+=10; // add and assign. it is same as num2 = num2+10. you can give a try to this equation too
System.out.println(num2); // we are just printing the result. expecting 20 as output. fingers crossed!
num1-=2;// this is subtract and assign. it is same as num1 = num1 – 2.
System.out.println(num1);// the result is expected to be 5 – 2 = 3. fingers crossed!
num2*=num1;// here, whatever is in num2 will be multiplied by whatever there is in num1.
// this is like num2 = num2 * num1. give it a try.
//In the above example. i wrote num1 in the right, just to mention that we can do this too.
System.out.println(num2);// expecting 20 * 3 = 60 as output.
num2/=3;// dividing whatever there is in num2 with 3 and assigning the result back to the variable.
System.out.println(num2);// the output expected is 20
num2%=3;// dividing whatever there is in num2 with 3, store the remainder into the variable num2
System.out.println(num2);// the output expected is 2
}
}
So, if we try executing the program above, the output we get is as follows →
20
3
60
20
2
So, I hope that you have understood the concept of assignment operators. Let’s brush up on the thing.
We have been using the simple assignment operator for a while right? It is just helping you assign some value to some variable.
Next comes the add and assign operator. With this operator, we are adding the value from the variable and some specified value (either a number or value from the variable) and storing it back in the variable.
So, if I do num1+=5; This is same as doing num1 = num1 + 5;
Here comes… the subtract and assign operator. With this operator, we are subtracting the value from the variable, with the specified value. So, if we do num1-=2; this is same as num1 = num1 – 2;
Now it’s the turn to multiply and assign an operator. With this operator, we are multiplying whatever there is in the variable with some specified value (or value from the variable), and assigning the result back to the variable. So, if we do num1*=4; this is same as num1 = num1 * 4;
The next assignment operator is the divide and assigns operator. With this operator, we are dividing whatever there is in the variable with some specified value (or the value from the variable), and assigning the result back to the variable. So, if we do num1/=2; this means that we are dividing whatever the value is in num1, with 2 and assigning the result back to num1. This is the same as num1 = num1/2;
The next assignment operator is modulo and assigns operator.
With this operator, we are dividing whatever there is in the variable with some specified value (or the value from the variable), and assigning the remainder of the division back to the variable. So, if we do num1%=2; we are just doing something the same as num1 = num1%2;
You can give a try the above examples, and try some more examples on your own. This will make you familiar with the use of assignment operators. We can use them as and when needed. Now let’s move on to the next type of operator. If you have any difficulty, please try the examples, read the description again, and explore a little bit more.