In Kotlin,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.
Number is an abstract base class that serves as the root of the numeric type hierarchy. It defines a strict contract for explicit type conversion. Unlike Java, Kotlin does not expose primitive types as distinct language keywords; instead, all numbers are objects at the language level, which the Kotlin compiler optimizes into JVM primitives at runtime whenever possible.
Built-in Numeric Types
Kotlin provides six built-in types that inherit from theNumber class, categorized by their bit width and memory representation.
Integer Types:
Byte: 8-bit signed integer.Short: 16-bit signed integer.Int: 32-bit signed integer (default for integer literals).Long: 64-bit signed integer.
Float: 32-bit single-precision floating-point (IEEE 754).Double: 64-bit double-precision floating-point (default for floating-point literals).
Char and Boolean are not numeric types in Kotlin and do not inherit from Number.)
The Number Class Contract
The Number abstract class forces all implementing numeric types to provide explicit conversion functions. Kotlin does not support implicit widening conversions (e.g., assigning an Int to a Long variable directly is a compilation error).
Literal Syntax and Type Inference
The Kotlin compiler infers the specificNumber subclass based on literal formatting and suffixes.
JVM Representation: Primitives vs. Boxed Types
On the JVM, the Kotlin compiler determines whether to represent aNumber as a primitive (e.g., int, double) or as a boxed object (e.g., java.lang.Integer, java.lang.Double).
- Primitive Representation: Used when the type is non-nullable and not used as a generic type argument. This avoids memory allocation overhead.
- Boxed Representation: Forced when the type is marked as nullable (
?) or used within generics.
===) behaves differently than structural equality (==).
Unsigned Numeric Types
Kotlin also provides unsigned integer types (UByte, UShort, UInt, ULong). Architecturally, these are implemented as inline classes wrapping their signed counterparts. They do not inherit from the Number abstract class, but they participate in the numeric ecosystem using specific literal suffixes (u or U).
Master Kotlin with Deep Grasping Methodology!Learn More





