0Pricing
Kotlin Academy · Lesson

Inline Value Classes

Wrap without overhead.

The Wrapping Problem

Wrapping a primitive in a class adds type safety but normally costs a heap allocation. Value classes give you the safety while the compiler erases the wrapper where possible.

Declaring a Value Class

Mark a class @JvmInline value class with exactly one property in the primary constructor. It behaves like a distinct type.

@JvmInline
value class Password(val value: String)

fun main() {
    val p = Password("secret")
    println(p.value)
}

All lessons in this course

  1. Type Aliases
  2. Inline Value Classes
  3. Use Cases
  4. Limitations
← Back to Kotlin Academy