Back to Blog
Java

Java Addition Subtraction Multiplication Division

java addition subtraction multiplication division: Learn how to perform addition, subtraction, multiplication, and division in Java, including integer truncation, floa...

Arithmetic OperatorsInteger DivisionBigDecimalFloating-Point PrecisionOperator Precedence
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 with primitive numeric types and 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 java addition subtraction multiplication division 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 and double operands:

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):

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. In that case, a + b is evaluated as string concatenation, not numeric addition.

Integer Division Truncates Toward Zero

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

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

This behavior is defined by the Java Language Specification. If you need rounding toward negative infinity or standard mathematical division, use Math.floorDiv():

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).

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 mathematical modulo that always returns a non-negative result, you can adjust:

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:

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 than double. If you need consistent results across platforms, prefer double unless memory constraints are severe.

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.

int result = 10 + 2 * 3; // 16, not 36 int another = (10 + 2) * 3; // 36 ```n 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 requires specifying a scale and rounding mode because the result may 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);

Always construct BigDecimal from a String rather than a double to avoid introducing binary floating-point errors. The divide method requires a scale and rounding mode; otherwise it throws an ArithmeticException if the result is non-terminating.

Overflow, Underflow, and Division by Zero

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

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:

int x = 5 / 0; // 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.

java addition subtraction multiplication division: Practical | RYUSLOG DEV