TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
/ operator in Kotlin is the arithmetic division operator, used to evaluate the quotient of two operands. At compile time, Kotlin translates the / operator into a method invocation of the div() member or extension function via operator overloading conventions.
Primitive Type Behavior
Signed and Unsigned Integer Division When both the dividend and the divisor are integer types (signed:Byte, Short, Int, Long; unsigned: UByte, UShort, UInt, ULong), the / operator performs integer division. Arithmetic operations on smaller types (Byte, Short, UByte, UShort) implicitly promote the operands to Int or UInt respectively. The return type resolves to the 64-bit variant (Long or ULong) only if at least one operand is 64-bit; otherwise, it resolves to the 32-bit variant (Int or UInt).
Evaluation truncates towards zero, strictly discarding any fractional component. Unsigned types utilize unsigned arithmetic instructions, yielding different results than signed division for bit-equivalent values.
Float or Double), the / operator performs floating-point division. The return type is implicitly promoted to the widest floating-point type present in the expression.
Division by Zero Mechanics
The behavior of the/ operator when the divisor evaluates to zero depends strictly on the operand types and the compilation target:
- Integer Types (JVM): Throws an
ArithmeticExceptionat runtime for both signed and unsigned integer division. - Integer Types (Kotlin/Native): Triggers a hardware trap or crash (e.g.,
SIGFPE/EXC_ARITHMETIC). For performance reasons, Kotlin/Native does not inject zero-checks into integer division operations, meaning it does not throw a catchable Kotlin exception. - 32-bit and Smaller Integer Types (Kotlin/JS): Does not throw an exception. Because Kotlin/JS maps 32-bit and smaller integers (
Int,Short,Byte) to JavaScript’sNumbertype (IEEE 754 floats) and emulates integer division using bitwise operations (e.g.,(a / b) | 0), dividing a non-zeroIntby0evaluates to0(sinceInfinity | 0evaluates to0). - 64-bit Integer Types (Kotlin/JS): Throws an
ArithmeticException. In Kotlin/JS, 64-bit integers (LongandULong) are not mapped to standard JavaScript numbers. They are emulated (or utilizeBigInt), and their division implementation explicitly checks for zero. - Floating-Point Types (All Targets): Adheres to the IEEE 754 standard and does not throw an exception. Instead, it evaluates to
Infinity(e.g.,1.0 / 0.0),-Infinity(e.g.,-1.0 / 0.0), orNaN(Not a Number, e.g.,0.0 / 0.0).
Operator Overloading
The/ operator can be implemented for user-defined types by declaring a function named div with the operator modifier. The function signature is highly flexible; the parameter type (divisor) and the return type (quotient) do not need to match the receiver type (dividend), allowing for asymmetric division operations.
Master Kotlin with Deep Grasping Methodology!Learn More





