Arithmetic Operators
In short: Operators for arithmetic operations: + (addition), - (subtraction), * (multiplication), / (division), and % (modulo/remainder).
In more detail: With / on two int operands, integer division is performed and the decimal part is dropped (7 / 2 gives 3, not 3.5) — for a true division, at least one operand must be a double/float. % returns the remainder of a division, often used to check e.g. even/odd numbers (n % 2 == 0).
In Depth
Integer division is one of the most common sources of error for Java beginners, because it silently behaves “wrong” without producing an error or a warning:
int a = 7, b = 2;
System.out.println(a / b); // 3 - integer division, NO error
System.out.println((double) a / b); // 3.5 - only the cast forces true division
System.out.println(a % b); // 1 - remainder of the division
// Classic even/odd check
boolean even = (a % 2 == 0);For negative numbers, / in Java rounds “towards zero” (not always downward as in some other languages): -7 / 2 gives -3, not -4. The % operator takes on the sign of the dividend for negative operands: -7 % 2 gives -1. For mixed data types (e.g. int and double), Java automatically converts the “smaller” type into the “larger” one (implicit type casting) before calculating — but for int / int, the expression stays int, even if the result is theoretically assigned to a double variable (double x = a / b; still returns 3.0, not 3.5, because the division is performed BEFORE the assignment).
Integer overflow: the second major trap
Integer types in Java have a fixed size (int, for example, 32 bits, value range roughly -2.1 to +2.1 billion). If a calculation exceeds this range, there’s NO exception — the value “overflows” silently and jumps to the other end of the value range:
int max = Integer.MAX_VALUE; // 2147483647
System.out.println(max + 1); // -2147483648 - silent overflow, no error!
long safer = (long) max + 1; // 2147483648 - cast BEFORE the addition prevents the overflowThis behaviour is a common cause of hard-to-find bugs in calculations with large numbers (e.g. multiplying two large int values) — when in doubt, prefer long or explicitly check for overflow (Math.addExact() throws an ArithmeticException on overflow instead of silently calculating wrong).
Order and parentheses
Java evaluates arithmetic expressions according to the well-known rule that multiplication/division come before addition/subtraction (*, /, % before +, -), with left-to-right evaluation for equal priority. For more complex expressions, explicit parentheses are often recommended despite correct rules — not because Java would calculate it wrong, but because it’s clearer for human readers:
int result = 10 + 5 * 2 - 3 % 2; // 10 + 10 - 1 = 19
int clearer = 10 + (5 * 2) - (3 % 2); // the same calculation, but immediately understandableFloating-point arithmetic: caution with comparisons
For double/float, rounding errors can occur due to the binary floating-point representation (0.1 + 0.2 internally gives 0.30000000000000004, not exactly 0.3). Direct equality comparisons (==) are therefore risky for floating-point numbers — a tolerance comparison (Math.abs(a - b) < 0.0001) is used instead.
See also: Operators, Type Casting, Operator Precedence