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

# Kotlin Number

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

```kotlin theme={"dark"}
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:**

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

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

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

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

```kotlin theme={"dark"}
val unsignedInt: UInt = 42u
val unsignedLong: ULong = 42uL
```

<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 Kotlin 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>
