Arithmetic Operators: *, /, %, +, – – Basic Elements, Primitive Data Types, and Operators

2.8 Arithmetic Operators: *, /, %, +, –

Arithmetic operators are used to construct mathematical expressions as in algebra. Their operands are of a numeric type (which includes the char type).

Floating-point operations are now consistently strict; that is, they are executed in accordance with the IEEE-754 32-bit (float) and 64-bit (double) standard formats. This means that floating-point arithmetic operations give the same results on any JVM implementation. The keyword strictfp used to enforce strict behavior for floating-point arithmetic is now obsolete and should not be used in new code.

Arithmetic Operator Precedence and Associativity

In Table 2.20, the precedence of the operators appears in decreasing order, starting from the top row, which has the highest precedence. Unary subtraction has higher precedence than multiplication. The operators in the same row have the same precedence. Binary multiplication, division, and remainder operators have the same precedence. The unary operators have right associativity, and the binary operators have left associativity.

Table 2.20 Arithmetic Operators

Unary+PlusMinus  
Binary*Multiplication/Division%Remainder
 +AdditionSubtraction  

Evaluation Order in Arithmetic Expressions

Java guarantees that the operands are fully evaluated from left to right before an arithmetic binary operator is applied. If evaluation of an operand results in an error, the subsequent operands will not be evaluated.

In the expression a + b * c, the operand a will always be fully evaluated before the operand b, which will always be fully evaluated before the operand c. However, the multiplication operator * will be applied before the addition operator +, respecting the precedence rules. Note that a, b, and c are arbitrary arithmetic expressions that have been determined to be the operands of the operators.

Example 2.1, p. 53, illustrates the evaluation order and precedence rules for arithmetic expressions.