Skip to content
Tutorials Center
My learning

Topics

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.

Java
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

Java Integer division and remainder

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.

Java Promotion, zero, and overflow

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

Java
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. int and long arithmetic wraps around in two's complement. Use Math.addExact, subtractExact, multiplyExact (or toIntExact) when overflow must be an error, and BigInteger for unbounded values.
  • Widen before you multiply: long total = a * b; with int a, b still multiplies as int. Cast one operand first: (long) a * b.
  • Integer.MIN_VALUE / -1 and Math.abs(Integer.MIN_VALUE) stay negative because the true result does not fit in an int.
  • Floating-point values are binary approximations. Do not compare with ==; compare with a tolerance. For money use BigDecimal or integer cents.
  • NaN is not equal to anything, including itself; use Double.isNaN(x).
  • Remainder is not modulo for negative numbers. For array indexes and clock arithmetic use Math.floorMod(a, n).
  • strictfp is 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 * 24 are folded by the compiler at no run-time cost; underscores in literals (86_400) improve readability.