2.1 Basic Arithmetic Operations

Many arithmetic expressions in Java look much like ordinary arithmetic expressions that you would see in a mathematics text. The four basic arithmetic operations are shown in the following table.

The normal precedence of operations applies, with multiplication and division being done (from left to right) before addition and subtraction (again, from left to right). Parentheses (but not square or brace brackets) can be used to change the order of evaluation. The multiplication symbol cannot be omitted as it sometimes is in mathematics. Operators can be written with or without extra spaces around them. Java also permits the use of unary plus and unary minus in which a + or - is placed in front of an expression. The unary plus and unary minus operators have a higher precedence than any of the other arithmetic operators.

Example 1

To evaluate the expression +4 + 3 * (5 - 6/2), Java would proceed as follows:

We use the symbol ⇒ rather than = in evaluations of expressions to avoid confusion with the use of = as an assignment operator. You may find it useful to read =} as "gives" or "evaluates to".

In Example 1, all the values were of type int and the result was also of type int. It is generally true that operations on values of the same type produce results of that type. In particular, if we divide two integers, the result is the integral quotient with any remainder being ignored. The values of the divisor or dividend may be either positive or negative. The sign of the quotient follows the usual rules of division.

Example 2

Whenever integer and floating point values are mixed in an expression, the floating point values are contagious so that the result is always a floating point value. In addition, if different integer types are mixed, the result is that of the widest type. The same is true if floating point types are mixed: the result of combining float and double values is always double.

Example 3

If conversions are necessary, Java does not convert all the original values in an expression prior to computing the result. Instead, it converts values or intermediate results as necessary as the computation proceeds.

Example 4

(a) 1+1.0/2 ⇒ 1+0.5 ⇒ 1.5
(b) 1.0+1/2 ⇒ 1.0+0 ⇒ 1.0
(c) 3/4*2.0 ⇒ 0*2.0 ⇒ 0.0

In mathematics, division by zero is undefined. If we are dividing one integer by another and the divisor has the value zero, then Java objects to this by throwing an exception. Many types of exceptions can be thrown by Java, depending on the error. For this error, Java throws an ArithmeticException. Throwing an exception has been likened to throwing up your hands and saying "I can't cope with this". If any exception is thrown during the execution of a program, then the programmer can write instructions to catch the exception. Appendix E contains notes on handling exceptions gracefully. For now, if a program contains this error, Java will throw an ArithmeticException and the program will terminate prematurely after printing a message stating the form of the exception.

If either operand of a division is a floating point value and an attempt is made to divide by zero, Java does not throw an exception. To handle results of such an operation, Java has three special floating point values: positive infinity, negative infinity, and NaN (not a number). The result positive infinity is produced by attempting to divide a positive value by zero while negative infinity is produced by attempting to divide a negative value by zero. These two values can be used in subsequent calculations. For example, adding a non-infinite value to positive infinity produces positive infinity. The value NaN is produced if we attempt to divide zero by zero. Attempts to print expressions having these values produce the strings Infinity, -Infinity, and NaN.

Example 5

Another useful arithmetic operator is written as %. The expression m % n gives the remainder after m is divided by n. We sometimes refer to m % n as m modulo n (or simply m mod n). If either operand is negative, the value of m % n is found by finding the value of |m| modulo |n| and then taking the sign of m. The % operator is usually used with integers but, in Java, it can also be used with floating point values. If the second operand is zero, then Java throws an exception for integer values but the expression evaluates to NaN for floating point values. In expressions with a number of different operators, the % operator has the same precedence as multiplication and division.

Example 6

(a) 7 % 3     ⇒ 1
(b) 12 % 15   ⇒ 12
(c) -20 % 7   ⇒ -6
(d) 5.9 % 1.2 ⇒ 1.1
(e) 8 % 0     Throws an ArithmeticException
(f) 0.8 % 0.0 ⇒ NaN

Exercise 2.1

  1. Evaluate each valid expression and state the type of the result. If the expression is invalid, give the reason. Use a decimal point in your answer if the result is a floating point value (either float or double). For example, use 3.0 rather than 3 to write a floating point result whose value is three.
    (a) 2 + 7 / 2 			(b) 5 (6 + 3.0)
    (c) 2e02 - 02  			(d) 3 * 5 / 2
    (e) 3.0 * 5 / 2 		(f) 2 * (3 / 4L)
    (g) 2.0 + 7 / (3 / 4)		(h) 3f * 4 / 5
    (i) 35 / (5 / 4)		(j) 4 - [6 - (2 - 7)]
    (k) 3 / 4 * 2.0			(1) 3 / (4 * 2.0) 
    
  2. Evaluate.
    (a) 17 % 5			(b) 23 % 10
    (c) 20 / 3 + 20 % 3		(d) -10 % 2 + 1 / 2
    (e) -7%(-2)/5			(f) 2 * (3 / 4L)
    (g) 7 % 1. 5			(h) 3f * 4 / 5
    (i) 1.5 % (3/4)			(j) 2 - 5/0.0
    (k) (-7)%(-3)			(l) 3 / (4 * 2.0)
    
  3. Delete any unnecessary parentheses.
    (a) ((a * b) / (c + d))
    (b) ((a * b) - (c % d))
    (c) ((a - b) % c) / (d * (e + f))
    (d) ((a * b) - (c / d) - (e / (f / g)))
    
  4. Write as Java expressions.

  5. Use the symbols +, -, *, /, %, (, and ) as often as you wish, together with the numerals 1, 8, 6, and 7, each used once, in the given order, to create Java expressions whose values are 0,1, ..., 10. As an example, to obtain the value zero, we could write
    1 / 8 + 6 / 7 ⇒ 0 + 0 ⇒ 0