Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Division operator /

The / operator computes a quotient. Operand types determine whether division is integer or floating-point, affecting fractional results and the behavior of division by zero.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

dividend / divisor

The binary / operator divides the left operand (dividend) by the right operand (divisor). Binary numeric promotion determines the calculation type: double, then float, then long, otherwise int.

Integer division truncates toward zero: 7 / 2 gives 3, and -7 / 2 gives -3. Assigning the result to double does not restore the lost fraction. Cast an operand before division: (double) a / b, not (double) (a / b).

If the operation is integer division after binary numeric promotion, a zero divisor causes ArithmeticException. A special overflow case is Integer.MIN_VALUE / -1, which returns Integer.MIN_VALUE without an exception; Long.MIN_VALUE / -1L behaves analogously.

Floating-point division may be inexact. It does not throw an arithmetic exception for division by zero: 1.0 / 0.0 gives Infinity, while 0.0 / 0.0 gives NaN. In 1.0 / 0, the divisor is promoted to double, so the result is also positive infinity (Infinity).

Examples

public class DivisionDemo {
    public static void main(String[] args) {
        int a = 7;
        int b = 2;
        System.out.println(a / b);
        System.out.println(-a / b);
        System.out.println((double) a / b);
        System.out.println((double) (a / b));
        System.out.println(1.0 / 0.0);
        System.out.println(0.0 / 0.0);
    }
}

Result

3
-3
3.5
3.0
Infinity
NaN

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources