[ Team LiB ] Page 97

[ Team LiB ] 3.5 Arithmetic Operators: *, /, %, +, The arithmetic operators are used to construct mathematical expressions as in algebra. Their operands are of numeric type (which includes the char type). Arithmetic Operator Precedence and Associativity In Table 3.3, the precedence of the operators is 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 3.3. Arithmetic Operators Unary + Addition -Subtraction Binary * Multiplication / Division % Remainder + Addition -Subtraction Evaluation Order in Arithmetic Expressions Java guarantees that the operands are fully evaluated from left to right before an arithmetic binary operator is applied. Of course, if evaluation of an operand causes an exception, 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 can be arbitrary arithmetic expressions that have been determined to be the operands of the operators. Range of Numeric Values As we have seen, all numeric types have a range of valid values (Section 2.2, p. 28). This range is given by the constants named MAX_VALUE and MIN_VALUE, which are defined in each numeric wrapper class. The arithmetic operators are overloaded, meaning that the operation of an operator varies depending on the type of its operands. Floating-point arithmetic is performed if any operand of an operator is of floating-point type, otherwise, integer arithmetic is performed. Values that are out-of-range or are results of invalid expressions, are handled differently depending on whether integer or floating-point arithmetic is performed. Integer Arithmetic Integer arithmetic always returns a value that is in range, except in the case of integer division by zero and remainder by zero, which causes an ArithmeticException (see the division operator / and the remainder operator % below). A valid value does not necessarily mean that the result is correct, as demonstrated by the following examples: int tooBig = Integer.MAX_VALUE + 1; // -2147483648 which is Page 98

Hint: This post is supported by Gama besplatan domen provider

Comments are closed.