Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Arithmetic: Addition, Subtraction, Multiplication, and Division

Learn how Java handles addition, subtraction, multiplication, and division, including integer truncation, remainder signs, floating-point precision, overflow, and division by zero.

~3 min readJava
#Arithmetic Operators#Integer Division#BigDecimal#Floating-Point Precision
Java arithmetic operators displayed on a code editor with a magnifying glass over the division sign, emphasizing precision and edge cases.

Java provides the standard arithmetic operators for addition (+), subtraction (-), multiplication (*), and division (/). These operators work on primitive numeric types and, through unboxing, on their wrapper classes, but the behavior of division differs significantly between integers and floating-point values. This article covers the syntax, common edge cases, and precision considerations when performing these operations in real code.

Basic Arithmetic Operators and Their Syntax

The four basic operators map directly to their mathematical counterparts. The following example demonstrates each operator with int operands:

java
int a = 10; int b = 3; int sum = a + b; // 13 int difference = a - b; // 7 int product = a * b; // 30 int quotient = a / b; // 3 (integer division)

When both operands are integers, Java performs integer division, which truncates any fractional part. To obtain a fractional result, at least one operand must be a floating-point type (float or double):

java
double quotient = 10.0 / 3; // 3.3333333333333335

The + operator also concatenates strings, so be careful when mixing numbers and strings in expressions like "Result: " + a + b. Because + is left-associative, this expression is evaluated as ("Result: " + a) + b, so the values of a and b are converted to strings and concatenated rather than added numerically.

Integer Division Truncates Toward Zero

Integer division in Java truncates toward zero, not toward negative infinity. This matters when one operand is negative:

java
int result = -7 / 2; // -3, not -4

This behavior is defined by the Java Language Specification. If you need floor division, use Math.floorDiv():

java
int floorResult = Math.floorDiv(-7, 2); // -4

For most applications, truncation toward zero is the expected behavior, but it can cause subtle bugs when dividing negative values. Always verify the sign of the operands when the result is used in a condition or index calculation.

The Remainder Operator (%) and Negative Operands

The remainder operator % returns the remainder of a division. Its behavior with negative operands is also defined by Java: the result has the same sign as the dividend (the left operand).

java
int remainder = 7 % 3; // 1 int negativeRemainder = -7 % 3; // -1 int positiveRemainder = 7 % -3; // 1

This is consistent with truncation toward zero. If you need a modulo operation that returns a non-negative result and the divisor is positive, you can use:

java
int mod = ((a % b) + b) % b;

This pattern is common when working with array indices or cyclic sequences.

Floating-Point Division and Precision Limits

When at least one operand is a double or float, division produces a floating-point result. However, binary floating-point cannot represent all decimal fractions exactly. For example:

java
double result = 1.0 / 3.0; // 0.3333333333333333

This is not a bug but a consequence of the IEEE 754 representation. For financial calculations or any scenario requiring exact decimal arithmetic, use BigDecimal instead of double.

Also note that float has lower precision and a smaller range than double. In most code, use double unless memory or storage size is a specific concern.

Operator Precedence and Parentheses

Java follows standard mathematical precedence: multiplication, division, and remainder have higher precedence than addition and subtraction. Operators with equal precedence are evaluated left-to-right.

java
int result = 10 + 2 * 3; // 16, not 36 int another = (10 + 2) * 3; // 36

When an expression mixes multiple operators, use parentheses to make the intent explicit. This reduces the chance of errors and improves readability, especially when the expression is not trivial.

Using BigDecimal for Exact Decimal Arithmetic

BigDecimal is the standard choice when exact decimal precision is required, such as in monetary calculations. It provides methods for addition, subtraction, multiplication, and division, but division may require specifying a scale and rounding mode because the result can be non-terminating.

java
import java.math.BigDecimal; import java.math.RoundingMode; BigDecimal a = new BigDecimal("10.50"); BigDecimal b = new BigDecimal("3.00"); BigDecimal sum = a.add(b); BigDecimal difference = a.subtract(b); BigDecimal product = a.multiply(b); BigDecimal quotient = a.divide(b, 2, RoundingMode.HALF_UP);

When you need to represent a decimal value exactly, construct BigDecimal from a String rather than from a double, because a double often contains a binary approximation of the decimal value. The divide overload shown here requires a scale and rounding mode; without them, an ArithmeticException can be thrown for non-terminating quotients.

Overflow and Division by Zero

Integer arithmetic in Java does not throw an exception on overflow; it silently wraps around. For example:

java
int max = Integer.MAX_VALUE; int overflow = max + 1; // -2147483648

This can lead to incorrect results in calculations that exceed the int or long range. Use Math.addExact(), Math.subtractExact(), Math.multiplyExact(), and Math.negateExact() to detect overflow and throw an ArithmeticException when it occurs.

Division by zero behaves differently for integers and floating-point numbers:

java
int divisor = 0; int x = 5 / divisor; // throws ArithmeticException double y = 5.0 / 0; // Infinity double z = 0.0 / 0; // NaN

Integer division by zero always throws ArithmeticException, so guard against it when the divisor comes from user input or an external source. Floating-point division by zero produces Infinity or NaN, which can propagate silently through calculations. Check for these values with Double.isInfinite() or Double.isNaN() when they are not expected.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.