W3docs

Java Arithmetic Operators

Use +, -, *, /, and % for arithmetic in Java, and understand operator precedence and integer vs. floating-point math.

Java has five arithmetic operators: addition, subtraction, multiplication, division, and remainder. Their syntax is the same as in C, JavaScript, or Python — but Java's static typing introduces a couple of surprises around division and overflow that are worth knowing before you write a single calculation.

The five operators

int a = 14;
int b = 4;

System.out.println(a + b);  // 18
System.out.println(a - b);  // 10
System.out.println(a * b);  // 56
System.out.println(a / b);  // 3   — integer division
System.out.println(a % b);  // 2   — remainder

Integer division truncates

When both operands of / are integers, the result is an integer and the fractional part is dropped (truncated toward zero):

System.out.println(7 / 2);    // 3, not 3.5
System.out.println(-7 / 2);   // -3
System.out.println(1 / 3);    // 0

To get a real-number result, at least one operand must be a floating-point type:

System.out.println(7.0 / 2);      // 3.5
System.out.println(7 / 2.0);      // 3.5
System.out.println((double) 7 / 2); // 3.5

This trips up beginners constantly. If you see 0 where you expected a non-zero quotient, suspect integer division.

The remainder operator %

% returns the remainder of integer division. It's defined so that (a / b) * b + (a % b) == a:

System.out.println(10 % 3);    // 1
System.out.println(-10 % 3);   // -1  — sign matches the dividend
System.out.println(10 % -3);   // 1

Common uses:

  • Test divisibility: if (n % 2 == 0) checks even.
  • Wrap-around indexing: arr[(i + 1) % arr.length].
  • Pad with zeros: see Java Math Class.

% also works on double, where it returns the IEEE 754 remainder — useful, but the Math.IEEEremainder method gives more precise control.

Operator precedence

The standard rules apply:

  1. *, /, % first
  2. then +, -
  3. left-to-right within the same precedence level
int result = 2 + 3 * 4;      // 14, not 20
int result2 = (2 + 3) * 4;   // 20
int result3 = 10 - 2 - 3;    // 5  — left-to-right

When in doubt, parenthesize. It costs nothing.

Increment and decrement — ++ and --

The unary ++ and -- increment or decrement a variable by 1:

int x = 5;
x++;     // x is 6
x--;     // x is 5

They come in prefix (++x) and postfix (x++) forms. The difference matters only inside an expression:

int a = 5;
int b = a++;   // postfix: b is 5 (old value), a becomes 6
int c = ++a;   // prefix: a becomes 7, c is 7

For readability, keep ++ and -- on their own line whenever you can.

Overflow wraps silently

Integer arithmetic on int and long wraps around on overflow — it does not throw:

int max = Integer.MAX_VALUE;    // 2147483647
int wrapped = max + 1;          // -2147483648 (Integer.MIN_VALUE)

If you need overflow detection, use Math.addExact, Math.multiplyExact, etc., which throw ArithmeticException on overflow.

Floating-point quirks

double is IEEE 754 — fast and well-supported, but it cannot represent most decimal fractions exactly:

System.out.println(0.1 + 0.2);   // 0.30000000000000004

This is not a Java bug — it's how binary floating-point works. For money, scientific precision, or anywhere you need exact decimal arithmetic, use BigDecimal:

import java.math.BigDecimal;

BigDecimal sum = new BigDecimal("0.1").add(new BigDecimal("0.2"));
System.out.println(sum);   // 0.3

(Always pass a string to new BigDecimal(...); new BigDecimal(0.1) would still pick up the binary inexactness.)

A demonstration

java— editable, runs on the server

What's next

Java Assignment Operators=, +=, and the compound assignment family.

Practice

Practice

What does (7 / 2) evaluate to in Java?