Here are some math operators you can use in your equations:
| Symbol | Operation |
|---|---|
| * | multiplication |
| / | division |
| + | addition |
| - | subtraction |
| ( ) | brackets to force order of operations BEDMAS |
| % | modulus - whole number division BUT instead of finding the answer, it finds the remainder. |
Example 1
class ArithmeticExpressions extends ConsoleProgram {
public void run() {
// Day Age Calculator
int intAge;
int intDays;
intAge = readInt("How old are you? ");
intDays = intAge * 365;
System.out.print("You have been alive for this many days: " + intDays);
}
}It is very common in programming to want to add one or subtract one to a variable. There are a couple ways to achieve this:
int counter = 1;
counter = counter + 1;Here, we are adding one to the counter variable. In the first line, counter is assigned the value 1. In the next line, it is reassigning the value of counter to 2.
Instead of writing counter = counter + 1. There's a shortcut:
counter++;
Similarly, we can use the same idea to decrement by 1:
counter--
Example 2
What would be stored in the daysUntilSummer variable?
int daysUntilSummer = 180;
daysUntilSummer--;It is also common to want to modify the current value by adding/subtracting/multiplying/dividing another value.
| Operation | Shortcut |
|---|---|
x = x + y |
x += y |
x = x - y |
x -= y |
x = x * y |
x *= y |
x = x / y |
x /= y |
BEDMAS as you would use in math class applies in code as well. This is the order of operations that applies:
- Parentheses
() - Multiplication and Division
*/% - Addition and Subtraction
+-
NOTE: If there are multiple instances of the same precedence, read left to right.
int/int=intint/double=doubledouble/int=doubledouble/double=double
Example 3
What do you think 5 / 2 is in Java? (if they are both integers)
Since int / int always results in an integer, it gets truncated (meaning the decimal gets removed)
Sometimes we want to change a value from one type to another.
This process is called casting.
We can add the type we want in parentheses to explicitly cast to that type.
When converting from a larger type to a smaller type (like double → int), Java does not do it automatically because information could be lost.
You must write the cast yourself. This is called a narrowing conversion.
Example 4
int x = (int)5.3; // x becomes 5Notice this truncates the decimal, it does not round.
Example 5
double x = 10.3;
int y = (int)x; // y becomes 10When converting from a smaller type to a larger type (like int → double), Java does it automatically.
This is called implicit casting or widening.
Example 6 (implicit)
int x = 5;
double y = x; // y becomes 5.0You can also write the cast explicitly, though it isn’t required:
Example 7 (explicit, but optional)
int x = 5;
double y = (double)x; // y also becomes 5.0Casting is especially important in arithmetic expressions.
Consider this example:
Example 8 (integer division issue)
int dollars = 100;
int numOfPeople = 40;
double dollarsPerPerson = dollars / numOfPeople;What do you think the result will be?
Answer
2.0Even though dollarsPerPerson is a double, the division is done using integers, so the result is 2, stored as 2.0.
To fix this, we can cast one of the operands to a double:
Example 9 (correct calculation)
int dollars = 100;
int numOfPeople = 40;
double dollarsPerPerson = (double)dollars / numOfPeople; // 2.5Now the calculation is done in floating-point arithmetic, giving the correct result.
| Conversion | Automatic? | Name | Example |
|---|---|---|---|
int → double |
Yes | Widening (implicit) | double y = x; |
int → double |
Optional | Explicit cast allowed | double y = (double)x; |
double → int |
No | Narrowing (explicit) | int y = (int)z; |