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.

In Kotlin, 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 the Number 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.
Floating-Point Types:
  • Float: 32-bit single-precision floating-point (IEEE 754).
  • Double: 64-bit double-precision floating-point (default for floating-point literals).
(Note: 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).
public abstract class Number {
    public abstract fun toDouble(): Double
    public abstract fun toFloat(): Float
    public abstract fun toLong(): Long
    public abstract fun toInt(): Int
    public abstract fun toShort(): Short
    public abstract fun toByte(): Byte
}
Explicit Conversion Syntax:
val intValue: Int = 42

// val longValue: Long = intValue // Compilation Error: Type mismatch
val longValue: Long = intValue.toLong() // Explicit conversion required
val byteValue: Byte = intValue.toByte() // Truncates if out of bounds

Literal Syntax and Type Inference

The Kotlin compiler infers the specific Number subclass based on literal formatting and suffixes.
val defaultInt = 42                 // Inferred as Int
val explicitLong = 42L              // Inferred as Long (requires 'L' suffix)
val hexInt = 0x2A                   // Hexadecimal prefix (0x)
val binInt = 0b101010               // Binary prefix (0b)
                                    // Note: Octal literals are not supported

val defaultDouble = 3.14            // Inferred as Double
val explicitFloat = 3.14f           // Inferred as Float (requires 'f' or 'F' suffix)
val expDouble = 3.14e2              // Scientific notation (Double)

val readableInt = 1_000_000         // Underscores permitted for readability

JVM Representation: Primitives vs. Boxed Types

On the JVM, the Kotlin compiler determines whether to represent a Number as a primitive (e.g., int, double) or as a boxed object (e.g., java.lang.Integer, java.lang.Double).
  1. Primitive Representation: Used when the type is non-nullable and not used as a generic type argument. This avoids memory allocation overhead.
  2. Boxed Representation: Forced when the type is marked as nullable (?) or used within generics.
// Compiled to JVM primitive 'int'
val primitiveInt: Int = 100 

// Compiled to JVM boxed 'java.lang.Integer' due to nullability
val boxedInt: Int? = 100 

// Compiled to JVM boxed 'java.lang.Integer' due to generics
val genericList: List<Int> = listOf(100) 
Because boxing creates object wrappers, identity equality (===) behaves differently than structural equality (==).
val a: Int = 10000
val b: Int = 10000
println(a === b) // true (primitive comparison)

val boxedA: Int? = a
val boxedB: Int? = a
println(boxedA == boxedB)  // true (structural equality, compares values)
println(boxedA === boxedB) // false (identity equality, compares memory references of boxed objects)

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).
val unsignedInt: UInt = 42u
val unsignedLong: ULong = 42uL
Master Kotlin with Deep Grasping Methodology!Learn More