Java Reference ยท Operators
Java arithmetic operators
Reference for Java arithmetic operators +, -, *, / and %: integer division, remainder with negatives, numeric promotion, division by zero and overflow.
Java has five binary arithmetic operators, + (addition), - (subtraction), * (multiplication), / (division), and % (remainder), plus unary + and -. They work on all numeric primitive types and on their wrapper classes, which are unboxed first. The + operator also concatenates strings; see String concatenation.
Two details cause most of the surprises: integer division throws away the fraction, and arithmetic on small types (byte, short, char) happens in int. Increment and decrement (++, --) have their own page.
int a = 7, b = 2; a + b; // 9 a / b; // 3 (integer division) a % b; // 1 a / 2.0; // 3.5 (b promoted to double)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
a + b a - b a * b a / b a % b +a -a
| Operator | Operands | Result |
|---|---|---|
+, -, * |
numeric | Sum, difference, product in the promoted type. |
/ |
integral | Quotient truncated toward zero; ArithmeticException if the divisor is 0. |
/ |
floating-point | IEEE 754 quotient; division by 0 gives Infinity, -Infinity, or NaN. |
% |
integral | Remainder with (a / b) * b + (a % b) == a; sign of the left operand. ArithmeticException if the divisor is 0. |
% |
floating-point | Remainder by truncated division, like C's fmod; NaN for a zero divisor. |
unary - |
numeric | Negation (promotes byte, short, char to int). |
Binary numeric promotion: if either operand is double, both become double; otherwise if either is float, both become float; otherwise if either is long, both become long; otherwise both become int. The result has that type.
Usage notes
- Integer overflow is silent.
intandlongarithmetic wraps around in two's complement. UseMath.addExact,subtractExact,multiplyExact(ortoIntExact) when overflow must be an error, andBigIntegerfor unbounded values. - Widen before you multiply:
long total = a * b;withint a, bstill multiplies asint. Cast one operand first:(long) a * b. Integer.MIN_VALUE / -1andMath.abs(Integer.MIN_VALUE)stay negative because the true result does not fit in anint.- Floating-point values are binary approximations. Do not compare with
==; compare with a tolerance. For money useBigDecimalor integer cents. NaNis not equal to anything, including itself; useDouble.isNaN(x).- Remainder is not modulo for negative numbers. For array indexes and clock arithmetic use
Math.floorMod(a, n). strictfpis no longer needed: since Java 17 floating-point arithmetic is always strict.- Compound assignment (
+=,-=,*=, ...) includes an implicit cast back to the left operand's type, which hides narrowing; see Assignment operators. - Precedence:
*,/,%bind tighter than+and-, and all are evaluated left to right. See Operator precedence. - Constant expressions such as
60 * 60 * 24are folded by the compiler at no run-time cost; underscores in literals (86_400) improve readability.