The Java language provides 7 operators that perform arithmetic on integer and floating point values.
+
operators:
+
operator that performs string concatenation. That is described in a separate example.)-
operators:
1. This is often incorrectly referred to as the "modulus" operator. "Remainder" is the term that is used by the JLS. "Modulus" and "remainder" are not the same thing.
The operators require numeric operands and produce numeric results. The operand types can be any primitive numeric type (i.e. byte
, short
, char
, int
, long
, float
or double
) or any numeric wrapper type define in java.lang
; i.e. (Byte
, Character
, Short
, Integer
, Long
, Float
or Double
.
The result type is determined base on the types of the operand or operands, as follows:
double
or Double
, then the result type is double
.float
or Float
, then the result type is float
.long
or Long
, then the result type is long
.int
. This covers byte
, short
and char
operands as well as `int.The result type of the operation determines how the arithmetic operation is performed, and how the operands are handled
double
, the operands are promoted to double
, and the operation is performed using 64-bit (double precision binary) IEE 754 floating point arithmetic.float
, the operands are promoted to float
, and the operation is performed using 32-bit (single precision binary) IEE 754 floating point arithmetic.long
, the operands are promoted to long
, and the operation is performed using 64-bit signed twos-complement binary integer arithmetic.int
, the operands are promoted to int
, and the operation is performed using 32-bit signed twos-complement binary integer arithmetic.Promotion is performed in two stages:
int
or long
is loss-less.float
to double
is loss-less.The / operator divides the left-hand operand n
(the dividend) and the right-hand operand d
(the divisor) and produces the result q
(the quotient).
Java integer division rounds towards zero. The JLS Section 15.17.2 specifies the behavior of Java integer division as follows:
The quotient produced for operands
n
andd
is an integer valueq
whose magnitude is as large as possible while satisfying|d ⋅ q| ≤ |n|
. Moreover,q
is positive when|n| ≥ |d|
andn
andd
have the same sign, butq
is negative when|n| ≥ |d|
andn
andd
have opposite signs.
There are a couple of special cases:
n
is MIN_VALUE
, and the divisor is -1, then integer overflow occurs and the result is MIN_VALUE
. No exception is thrown in this case.d
is 0, then `ArithmeticException is thrown.Java floating point division has more edge cases to consider. However the basic idea is that the result q
is the value that is closest to satisfying d . q = n
.
Floating point division will never result in an exception. Instead, operations that divide by zero result in an INF and NaN values; see below.
Unlike C and C++, the remainder operator in Java works with both integer and floating point operations.
For integer cases, the result of a % b
is defined to be the number r
such that (a / b) * b + r
is equal to a
, where /
, *
and +
are the appropriate Java integer operators. This applies in all cases except when b
is zero. That case, remainder results in an ArithmeticException
.
It follows from the above definition that a % b
can be negative only if a
is negative, and it be positive only if a
is positive. Moreover, the magnitude of a % b
is always less than the magnitude of b
.
Floating point remainder operation is a generalization of the integer case. The result of a % b
is the remainder r
is defined by the mathematical relation r = a - (b ⋅ q)
where:
q
is an integer,a / b
is negative an positive only if a / b
is positive, anda
and b
.Floating point remainder can produce INF
and NaN
values in edge-cases such as when b
is zero; see below. It will not throw an exception.
Important note:
The result of a floating-point remainder operation as computed by
%
is not the same as that produced by the remainder operation defined by IEEE 754. The IEEE 754 remainder may be computed using theMath.IEEEremainder
library method.
Java 32 and 64 bit integer values are signed and use twos-complement binary representation. For example, the range of numbers representable as (32 bit) int
-231 through +231 - 1.
When you add, subtract or multiple two N bit integers (N == 32 or 64), the result of the operation may be too large to represent as an N bit integer. In this case, the operation leads to integer overflow, and the result can be computed as follows:
It should be noted that integer overflow does not result in exceptions under any circumstances.
Java uses IEE 754 floating point representations for float
and double
. These representations have some special values for representing values that fall outside of the domain of Real numbers:
+INF
value denote numbers that are too large and positive. The -INF
value denote numbers that are too large and negative.The INF values are produced by floating operations that cause overflow, or by division by zero.
The NaN values are produced by dividing zero by zero, or computing zero remainder zero.
Surprisingly, it is possible perform arithmetic using INF and NaN operands without triggering exceptions. For example:
For full details, please refer to the relevant subsections of JLS 15. Note that this is largely "academic". For typical calculations, an INF
or NaN
means that something has gone wrong; e.g. you have incomplete or incorrect input data, or the calculation has been programmed incorrectly.