Skip to content

VOL 01 / CH 03 / LESSON 02

3.2 Arithmetic, Division, and Remainders ​

Prerequisites: Expressions, Assignments, and Evaluation Order

Estimated time: 25 minutes

You are ready to continue when: You can predict integer division, remainders, overflow, and side effects of increment operators

Basic Arithmetic and Types ​

java
int left = 10;
int right = 3;

System.out.println(left + right); // 13
System.out.println(left - right); // 7
System.out.println(left * right); // 30
System.out.println(left / right); // 3
System.out.println(left % right); // 1

When participating in ordinary arithmetic, byte, short, and char are typically promoted to int. Expressions may also overflow, and types do not automatically upgrade due to the result being too large.

Integer Division Truncating Toward Zero ​

java
System.out.println(10 / 3);   // 3
System.out.println(-10 / 3);  // -3
System.out.println(10 / -3);  // -3
System.out.println(-10 / -3); // 3

“Drop the decimal part” is an informal shortcut. The precise rule, including negative operands, is to truncate the quotient toward zero.

When at least one operand is a floating-point type, a floating-point division is performed:

java
System.out.println(10 / 3.0);        // 3.3333333333333335
System.out.println((double) 10 / 3); // 3.3333333333333335

Assigning to double does not propagate to the right side of the operation:

java
double wrong = 10 / 3;   // Compute int 3 first, then widen to 3.0
double right = 10 / 3.0;

Integer division by zero throws ArithmeticException; floating-point division by positive zero or negative zero produces infinity or NaN according to IEEE 754:

java
System.out.println(1.0 / 0.0); // Infinity
System.out.println(0.0 / 0.0); // NaN

There is also an integer boundary: the mathematical result of Integer.MIN_VALUE / -1 exceeds int, and Java returns Integer.MIN_VALUE. Math.floorDiv does not detect this overflow either. In Java 17, check this input pair explicitly or convert an operand to long before dividing.

% is the remainder, not necessarily non-negative modulo ​

In Java, the remainder satisfies:

text
(a / b) * b + (a % b) == a

This uses Java integer arithmetic and requires a nonzero divisor. Even when MIN_VALUE / -1 overflows, the identity holds under Java’s wraparound rules. A nonzero remainder has the sign of the dividend:

java
System.out.println(7 % 3);   // 1
System.out.println(-7 % 3);  // -1
System.out.println(7 % -3);  // 1

Therefore, to check for an odd number, you should write:

java
boolean odd = value % 2 != 0;

value % 2 == 1 will miss negative odd numbers.

For a positive modulus, use this to obtain an index in 0..modulus-1:

java
int wrapped = Math.floorMod(index, 3);
java
System.out.println(-1 % 3);             // -1
System.out.println(Math.floorMod(-1, 3)); // 2

Pagination also requires defining the numbering convention in advance. If the entries start from 0, index / pageSize is the page index; if the business numbering starts from 1, the formula is typically (number - 1) / pageSize. Do not just paste one % and call it a general pagination.

Increment and Decrement Are Side-Effectful Expressions ​

java
int count = 5;
int before = count++; // before=5, count=6
int after = ++count;  // count=7, after=7
  • The value of an expression with a postfix count++ is the value before the variable is incremented, and then the variable is increased by one;
  • The value of an expression with a prefix ++count is the value after the variable is modified, and the expression value is the modified value.

The count++ is very common in loop updates. Do not read and write the same variable multiple times within the same complex expression:

java
int result = count++ + ++count; // Rules are defined but have high maintenance costs

Splitting it can preserve the intent:

java
int first = count;
count++;
count++;
int second = count;
int result = first + second;

Overflow and exact Methods ​

java
int area = width * height;

Even if width and height are both valid, the product may overflow. When failure is required rather than wrapping:

java
int area = Math.multiplyExact(width, height);

If you first cast to long:

java
long area = (long) width * height;

The cast must come before the multiplication; long area = width * height will first compute using int and may have already overflowed.

Professional Reference ​

ScenarioRule
Integer divisionTruncate toward zero
Floating-point division by zeroInfinity or NaN, not throw integer division by zero exception
Remainder signSame as the dividend
Non-negative loop indexMath.floorMod with a positive modulus
Negative parityvalue % 2 != 0
Multiplication promoted to longConvert one operand before operation
To detect overflowMath.*Exact or explicit boundary check
++/--Simple update is available, avoid mixing with other side effects

Before Moving On ​

Use -10 / 3, -7 % 3, and Math.floorMod(-1, 3) to check if you can accurately write the rules for negative numbers; then explain why long result = intValue * otherInt might still overflow first.

Arithmetic produces values, but programs also need to combine conditions.

→ Next: Comparisons, Equality, and Short-Circuit Logic

Specification: JLS §15.17: Multiplication, Division, and Remainder, §15.14.2: Postfix Increment.

Built with VitePress | Software Systems Atlas