A common use for expressions is to perform arithmetic on variables: addition, subtraction, multiplication, or division. The following table describes the operators commonly used for arithmetic.
|
Operator
|
Description
|
Example
|
| + (addition) | Returns the sum of two operands | 5 + 4 |
| - (subtraction) | Returns the difference of two operands | 5 - 4 |
| * (multiplication) | Returns the product of two operands | 5 * 4 |
| / (division) | Returns the quotient of two operands | 5 / 4 |
The kind of variable that you use when performing arithmetic can affect the result. Dividing two numbers often results in a return value that is not a whole number. For example, when you divide 3 by 2, the result is 1.5. If you assigned the return value of that expression to an Integer variable, it would be rounded to the nearest whole number, 2. When performing division, you should use a Double variable to store the return value.