Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The % operator in Java is the arithmetic remainder operator. It evaluates the remainder of dividing the left-hand operand (dividend) by the right-hand operand (divisor). While frequently referred to as the modulo operator, Java’s implementation strictly calculates the remainder via truncated division. This means the sign of the resulting value is determined entirely by the dividend, ignoring the sign of the divisor.
int dividend = 10;
int divisor = 3;
int result = dividend % divisor;

Operand Compatibility

The % operator accepts all primitive numeric types, including integer types (byte, short, char, int, long) and floating-point types (float, double). It also accepts their corresponding object wrapper classes (e.g., Integer, Double) via automatic unboxing. Standard binary numeric promotion applies to the operands before the operation is executed.

Integer Remainder Mechanics

For integer operands, the % operator satisfies the following identity, which the Java Language Specification guarantees will hold true even if the division operation overflows: (a / b) * b + (a % b) == a Because Java uses truncated division (rounding towards zero), the remainder inherits the sign of the left-hand operand (a).
int a =  5 %  3;  // Evaluates to  2
int b = -5 %  3;  // Evaluates to -2
int c =  5 % -3;  // Evaluates to  2
int d = -5 % -3;  // Evaluates to -2

Floating-Point Remainder Mechanics

Unlike C and C++, Java permits the % operator on floating-point operands. The operation computes a truncating remainder analogous to the integer remainder operator. The result is calculated as a - (b * q), where q is the integer portion of a / b truncated toward zero. The % operator explicitly does not compute the IEEE 754 remainder, which uses a rounding division (round to nearest). To achieve true IEEE 754 remainder behavior in Java, developers must use Math.IEEEremainder().
double x = 5.5 % 2.0;   // Evaluates to 1.5
double y = -5.5 % 2.0;  // Evaluates to -1.5

Edge Cases and Exceptions

The behavior of the % operator changes significantly depending on whether the operands are integers or floating-point numbers when encountering zero or infinite values. Integer Exceptions:
  • Division by Zero: If the right-hand operand is 0, the JVM throws an ArithmeticException at runtime.
try {
    int result = 5 % 0; 
} catch (ArithmeticException e) {
    // Catches java.lang.ArithmeticException: / by zero
}
Floating-Point Special Values: Floating-point remainder operations never throw an ArithmeticException. Instead, they return specific values defined by the Java Language Specification:
  • NaN Operands: If either operand is NaN, the result is NaN.
  • Infinite Dividend or Zero Divisor: If the dividend is Infinity or -Infinity, or if the divisor is 0.0 or -0.0, or both, the result is NaN. This guarantees that operations like Infinity % Infinity evaluate to NaN.
  • Finite Dividend and Infinite Divisor: If the dividend is finite and the divisor is Infinity or -Infinity, the result is equal to the dividend.
  • Zero Dividend and Finite Divisor: If the dividend is 0.0 or -0.0 and the divisor is finite, the result is equal to the dividend.
double res1 = 5.0 % 0.0;                                           // Evaluates to Double.NaN
double res2 = Double.POSITIVE_INFINITY % 2.0;                      // Evaluates to Double.NaN
double res3 = Double.POSITIVE_INFINITY % Double.POSITIVE_INFINITY; // Evaluates to Double.NaN
double res4 = 5.0 % Double.POSITIVE_INFINITY;                      // Evaluates to 5.0
Master Java with Deep Grasping Methodology!Learn More