Java provides five math operators, familiar from mathematics:
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder of integer/floating point division) |
Addition, subtraction, and multiplication work like you've seen elsewhere:
2 + 5 = 7
5.2 + 8.1 = 13.3
4 - (-2) = 6
13.23 - 1.179 = 12.051
10 * 8 = 80
4.82 * 4 = 19.28
Division can be trickier because if you're dividing two
11 / 2 = 5
// int division; the 0.666666.. is dropped
11.0 / 3 = 3.66666666..
11 / 3.0 = 3.66666666..
The modulus operator returns the remainder of dividing the first number by the second:
10 % 3 = 1
20 % 2 = 0
1 % 3 = 1
25.5 % 2.25 = 0.75