-
Notifications
You must be signed in to change notification settings - Fork 0
Calculations
Performing arithmetic operations is one of the cornerstones of programming. Almost every program you will run across will have to perform math or some sort of calculation. To do this we must first know what the available math operators for Java.
Symbol | What it does |
---|---|
+ | Adds two numbers together |
- | Subtracts number from another |
/ | Divides one number from another |
* | Multiplies two numbers together |
% | Finds the remainder of a number from another |
Using operators is just as simple as adding the operators on paper. Let's say you want to add 2 to 4. To do that all you need to do is put the addition operator between the two numbers.
Example:
System.out.println(2 + 4);
Output:
6
This same structure you can subtract, multiply and divide variables. The Java programming language also has an order of operations. To use this order of operations you can use parentheses to specify which operation. This is not required, however, because Java takes into account regular order of operations.
Example:
System.out.println(1 * 2 + 3)
**Output: **
5
System.out.println(1 + 2 * 3)
Output:
7
For the code above the two code snippets give different output because of the different position of the multiplication symbol. The first line is the same as calculating (1*2) + 3 and the second operation is the same as writing 1 + (2 * 3). Of course, you can make these operations as complex or as simple as necessary but the more complex a calculation the more confusing it gets.
Just like you can use variable to store values, you can also use variables to perform math with those variables. Doing so is just as simple as adding two numbers. Simply just use arithmetic operators too add, sub multiply etc. values to or from the variable.
Example:
int sumNum = 5;
System.out.println(sumNum + 4);
Output:
9
You can also add variable together.
int numOne = 5;
int numTwo = 11;
System.out.println(x + y);
Output:
16
If you wanted to add more variable you simply continue to add them to the statement. The same rules that apply regular numbers also apply to variables that store numbers as well. You can even perform math and then set that value equal to a variable.
Example:
int firstNum = 1;
int secondNum = 9;
int sum = 0;
sum = x + y;
System.out.println(sum);
Output:
10
If you quickly need to change the value of a variable you can use what is know as a shorthand operator. These shorthand operators allow you to change the value of a variable without having to declare another variable. The syntax for shorthand operators is the arithmetic symbol followed by the equals sign. For example, the addition shorthand operator is +=
. To use this operator you would write the variable followed by the operator and then the value to perform the operation with.
Example:
int number = 7;
number += 4;
System.out.println(number)
Output:
11
Shorthand operators are not only limited to addition below is a table containing all shorthand operators.
Operator | Operator Name | Example | Meaning |
---|---|---|---|
+= | Addition | x += 4 | x = x + 4 |
-= | Subtraction | x -= 4 | x = x - 4 |
*= | Multiplication | x *= 4 | x = x * 4 |
/= | Division | x /= 4 | x = x / 4 |
%= | Remainder | x %= 4 | x = x % 4 |
Postfix and prefix operators are another easy way to quickly perform math operations. However, pre and postfix operators only apply to addition and subtraction. The operators also only increase or decrease the variable by 1. The syntax for these operators is just a set of the operators (++ for addition and -- for subtraction). What makes the operation a pre of postfix is where it is placed on the variable. Placing the operator before the variable makes it a prefix (++x) and placing it after makes it a postfix (x++). No matter which operator you use they do they same thing. They either increment or decrement the variable. However, how they differ is that they perform this operation at different times.
Example:
Prefix
int preNum = 5;
System.out.println(++preNum);
Output:
6
Postfix:
int postNum = 10
System.out.println(postNum++);
Output:
10
For prefix operator, you can see that the variable is successfully increased by one. However, for the postfix operator, it appears that nothing happened. This is because postfix operator executes AFTER the variable is output. If you wrote another print statement that output the variable then you would see that the variable was incremented.