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

# Java Remainder

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.

```java theme={"dark"}
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`).

```java theme={"dark"}
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()`.

```java theme={"dark"}
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.

```java theme={"dark"}
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.

```java theme={"dark"}
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
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
