Unary Arithmetic Operators: -, +
The unary operators have the highest precedence of all the arithmetic operators. The unary operator – negates the numeric value of its operand. The following example illustrates the right associativity of the unary operators:
int value = – -10; // (-(-10)) is 10
Notice the blank space needed to separate the unary operators; without the blank space, these would be interpreted as the decrement operator — (p. 69), which would result in a compile-time error because a literal cannot be decremented. The unary operator + has no effect on the evaluation of the operand value.
Multiplicative Binary Operators: *, /, %
Multiplication Operator:*
The multiplication operator * multiplies two numbers, as one would expect.
int sameSigns = -4 * -8; // result: 32
double oppositeSigns = 4 * -8.0; // Widening of int 4 to double. result: -32.0
int zero = 0 * -0; // result: 0
Division Operator: /
The division operator / is overloaded. If its operands are integral, the operation results in integer division.
int i1 = 4 / 5; // result: 0
int i2 = 8 / 8; // result: 1
double d1 = 12 / 8; // result: 1.0; integer division, then widening conversion
Integer division always returns the quotient as an integer value; that is, the result is truncated toward zero. Note that the division performed is integer division if the operands have integral values, even if the result will be stored in a floating-point type. The integer value is subjected to a widening conversion in the assignment context.
An ArithmeticException is thrown when integer division with zero is attempted, meaning that integer division by zero is an illegal operation.
If any of the operands is a floating-point type, the operation performs floating-point division, where relevant operand values undergo binary numeric promotion:
double d2 = 4.0 / 8; // result: 0.5
double d3 = 8 / 8.0; // result: 1.0
float d4 = 12.0F / 8; // result: 1.5F
double result1 = 12.0 / 4.0 * 3.0; // ((12.0 / 4.0) * 3.0) which is 9.0
double result2 = 12.0 * 3.0 / 4.0; // ((12.0 * 3.0) / 4.0) which is 9.0