Arithmetic and Assignment Operators
Arithmetic operators are used to modify the value of variables by performing math opperations on them. The (+) operator is used for addition. Let’s use our “MyVariables” program for this example.
public class MyVariables { public static void main(String[] args) {int myCoolVariable = 5 + 8;
System.out.println(myCoolVariable);
} }
The (+) operator does its math and the output is “13.”
You can even use variable names with operators. Like this:
public class MyVariables { public static void main(String[] args) {int myCoolVariable = 5 + 8;
myCoolVariable =
myCoolVariable
+ 2;System.out.println(myCoolVariable);
} }
Now the output is “15.”
This (–) is the subtraction operator. This (*) is for multiplication. Use (/) for division. And this guy (%) is called the modulus operator. It is used to return the remainder of a division problem. Let’s check it out.
public class MyVariables { public static void main(String[] args) {int myCoolVariable = 10 % 3;
System.out.println(myCoolVariable);
} }
The output now is “1” since 10 can be divided by 3 three times with a remainder of 1.
The last two arithmetic operators are the increment (++) and the decrement (— —) operators. The (++) increment will increase a variable’s value by 1, and the (— —) decrement will decrease a variable’s value by 1. Here is an example:
public class MyVariables { public static void main(String[] args) {int myCoolVariable = 3;
myCoolVariable--;
//Decrease the value by 1.
System.out.println(myCoolVariable);
} }
Now the output is “2.”
One final note about the (++) increment and (— —) decrement operators. They can be placed either before a variable name or after. The behavior is slightly different depending on where you place them. Look at this example:
public class MyVariables { public static void main(String[] args) {int myCoolVariable = 3;
System.out.println(++myCoolVariable);
} }
Here the value of “myCoolVariable” gets increased by 1 and then output. So the output is “4.” But in this next example:
public class MyVariables { public static void main(String[] args) {int myCoolVariable = 3;
System.out.println(myCoolVariable++);
} }
This time the output is “3” because “myCoolVariable” gets output first and then the value gets increased by 1.
You already know one of the assignment operators. The (=) is used to assign a value to a variable.
Another one is the (+=) operator. This operator takes the current value of a variable and adds to it. Like this:
public class MyVariables { public static void main(String[] args) {int myCoolVariable = 3;
myCoolVariable += 5;
System.out.println(myCoolVariable);
} }
The 5 gets added to “myCoolVariable.” Then the output is “8.”
The (-=) operator takes the current value of a variable and subtracts from it.
The (*=) operator takes the current value of a variable and multiplies it.
The (/=) operator takes the current value of a variable and divides it.
The (%=) operator takes the current value of a variable and divides it, then assigns the value of any remainder to the variable.
Challenge:
Look at this example:
public class MyVariables { public static void main(String[] args) {int myCoolVariable = 30;
myCoolVariable %= 7;
System.out.println(myCoolVariable++);
} }
Can you guess what the output will be?
Will the final value of “myCoolVariable” be the same as the output?
Next Topic: Comparison and Logical Operators
Comments
Post a Comment