Java’s modulo operator returns the remainder after division, with the result’s sign matching the left operand.
The modulo operator in Java is written as %. It answers a plain question: after one number is divided by another, what is left over? If 17 / 5 gives 3 full groups, 17 % 5 gives 2, because 2 is left after taking away three groups of 5.
Java calls % the remainder operator, and that wording matters. Many developers say “modulo” in daily coding, but Java’s behavior follows remainder rules, not the always-positive math modulo rule some people expect.
Why The % Operator Matters In Daily Java Code
The % operator is small, but it shows up all over real programs. You’ll see it in counters, array rotation, odd-even checks, clock math, cyclic patterns, pagination, hashing, and simple divisibility tests.
Here are common tasks where it fits well:
- Check if a number is even:
n % 2 == 0 - Check if a value is divisible by 10:
n % 10 == 0 - Wrap an index around a fixed size
- Split work across buckets
- Pull the last digit of a positive integer
Oracle lists % with the rest of Java’s arithmetic operators in its Java operators lesson. The operator works with numeric types, including integers and floating-point values, though integer code is where most beginner mistakes happen.
How Does Modulo Work In Java? With Negative Numbers
For positive numbers, the result feels natural. 10 % 3 is 1. 20 % 4 is 0. No surprise there.
Negative numbers are where Java trips people up. Java integer division moves toward zero. The remainder is whatever keeps this relationship true:
(a / b) * b + (a % b) == a
So -10 / 3 is -3, not -4. Then Java solves the rest like this:
(-3 * 3) + (-1) == -10
That means -10 % 3 is -1. The remainder carries the sign of the left operand, also called the dividend. Oracle’s Java Language Specification remainder rule states the exact behavior for integer and floating-point remainder operations.
This sign rule is the main reason Java modulo code can fail when indexes go below zero. Code that works fine for positive inputs may break on refunds, countdowns, offsets, time shifts, or reverse movement.
Modulo Results You Can Check At A Glance
The table below shows how Java thinks through common cases. The left operand decides the sign of the remainder, and a zero result means the division has no leftover amount.
| Expression | Java Result | What It Means |
|---|---|---|
10 % 3 |
1 |
Three groups of 3 fit, with 1 left. |
10 % 5 |
0 |
5 divides 10 cleanly. |
7 % 10 |
7 |
The divisor is larger, so the left value remains. |
-10 % 3 |
-1 |
The left operand is negative, so the remainder is negative. |
10 % -3 |
1 |
The left operand is positive, so the remainder is positive. |
-10 % -3 |
-1 |
The left operand is negative, so the result stays negative. |
0 % 4 |
0 |
Zero divided by 4 leaves nothing. |
4 % 0 |
ArithmeticException |
Integer remainder by zero is not allowed. |
Using Remainders For Odd, Even, And Divisibility Checks
The easiest modulo pattern in Java is the odd-even check. A number is even when division by 2 leaves no remainder.
int n = 42;
if (n % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
For positive and negative integers, this still works. -8 % 2 is 0, so -8 is even. For odd checks, many developers write n % 2 != 0 rather than n % 2 == 1, because negative odd values return -1, not 1.
boolean isOdd = n % 2 != 0;
The same pattern works for any divisor:
n % 3 == 0checks divisibility by 3.n % 5 == 0checks divisibility by 5.n % 100 == 0checks clean division by 100.
When Math.floorMod Is The Safer Choice
If you want a wraparound result that stays within a positive range, % may not be enough. Use Math.floorMod when negative inputs should still land inside a fixed cycle.
int size = 5;
System.out.println(-1 % size); // -1
System.out.println(Math.floorMod(-1, size)); // 4
This difference matters for array indexes. An index of -1 crashes when used directly. An index of 4 wraps to the last slot, which is often what circular code wants. Oracle’s Math.floorMod documentation explains that its result is based on floor division rather than division that moves toward zero.
Common Java Modulo Tasks And Safer Patterns
Use the plain remainder operator when you care about divisibility or leftovers. Use Math.floorMod when you care about wraparound placement.
| Task | Good Pattern | Why It Works |
|---|---|---|
| Even check | n % 2 == 0 |
Even numbers leave no remainder. |
| Odd check | n % 2 != 0 |
Works for positive and negative odd values. |
| Clean division | n % divisor == 0 |
A zero remainder means no leftover. |
| Circular index | Math.floorMod(i, size) |
Keeps negative positions inside the range. |
| Last digit, positive number | n % 10 |
Returns the leftover after groups of 10. |
Floating-Point Modulo Has Its Own Traps
Java also allows % with float and double. That can be handy for small numeric tasks, but decimal values are stored in binary, so results can contain tiny rounding noise.
double r = 5.5 % 2.0;
System.out.println(r); // 1.5
For money, don’t build remainder logic with double. Use integer cents or BigDecimal, depending on the job. Decimal rounding bugs are painful because they can pass casual tests and fail near boundaries.
Rules That Prevent Modulo Bugs
The best way to write reliable Java remainder code is to be clear about the goal before typing %. Are you checking leftovers, or are you wrapping around a range?
These rules handle most cases:
- Use
%for divisibility checks. - Use
n % 2 != 0for odd checks, notn % 2 == 1. - Never use an integer divisor that might be zero.
- Use
Math.floorModfor circular indexes with negative input. - Test negative values when a number can move backward.
- Be cautious with
doubleandfloatremainders.
Java modulo is easy once the sign rule clicks. The % operator returns a remainder, not a guaranteed positive wraparound value. That single detail explains most confusing results and points you toward Math.floorMod when wraparound behavior is the real goal.
References & Sources
- Oracle Java Tutorials.“Operators.”Lists Java arithmetic operators, including the remainder operator.
- Oracle Java Language Specification.“Remainder Operator %.”Defines Java’s exact remainder rules for numeric operands.
- Oracle Java API Documentation.“Math.floorMod.”Explains floor modulus behavior for integer wraparound code.
