0Pricing
Kotlin Academy · Lesson

Inline Value Classes

Wrap without overhead.

Inline Value Classes is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

A Distinct Type

Unlike a type alias, a value class is a separate type. You cannot accidentally pass a raw String where a Password is expected.

@JvmInline
value class Password(val value: String)

fun login(p: Password) = println("login with " + p.value)

fun main() {
    login(Password("hunter2"))
}

Zero Overhead

At runtime the compiler usually inlines the underlying value, so there is no extra object allocation. A Password is represented as a plain String in most code paths.

@JvmInline
value class Meters(val amount: Double)

fun main() {
    val d = Meters(5.0)
    println(d.amount) // stored inline as a Double
}

Methods and Properties

Value classes can have functions and computed properties, just like normal classes.

@JvmInline
value class Celsius(val degrees: Double) {
    fun toFahrenheit() = degrees * 9 / 5 + 32
}

fun main() {
    println(Celsius(100.0).toFahrenheit())
}

Init Blocks for Validation

An init block lets you validate the wrapped value on construction, guaranteeing every instance is valid.

@JvmInline
value class Percentage(val value: Int) {
    init {
        require(value in 0..100) { "out of range" }
    }
}

fun main() {
    println(Percentage(75).value)
}

Computed Properties

You can add computed properties that derive from the single stored value, keeping logic close to the type.

@JvmInline
value class Seconds(val value: Int) {
    val asMinutes: Double get() = value / 60.0
}

fun main() {
    println(Seconds(120).asMinutes)
}

Implementing Interfaces

Value classes can implement interfaces, allowing polymorphic use. Note: doing so may force boxing when treated as the interface type.

interface Describable {
    fun describe(): String
}

@JvmInline
value class Money(val cents: Int) : Describable {
    override fun describe() = "$" + cents / 100.0
}

fun main() {
    println(Money(1599).describe())
}

When Boxing Happens

The wrapper reappears (boxing) when the value is used as a nullable, as an interface, or in a generic collection. In those cases the compiler creates a real object — still correct, just less efficient.

@JvmInline
value class Id(val raw: Int)

fun main() {
    val direct = Id(1)        // inlined
    val boxed: Id? = Id(2)    // boxed because nullable
    println("" + direct.raw + " " + boxed?.raw)
}

value vs data class

A data class always allocates and can hold many properties. A value class holds exactly one and aims for zero overhead. Choose value classes for thin, type-safe wrappers around a single value.

Why It Matters

Value classes let you:

  • Prevent mixing up like-typed parameters
  • Attach validation to a primitive
  • Pay (usually) no runtime cost

They are the type-safe answer where aliases fall short.

Quick Check

Test your understanding of inline value classes.

Recap

You learned inline value classes:

  • Declared @JvmInline value class with one property
  • A distinct type with (usually) zero overhead
  • Support methods, computed props, and init validation
  • Boxing occurs when nullable, generic, or used as an interface

Next: real-world use cases.

Frequently asked questions

Is the “Inline Value Classes” lesson free?

Yes — the full text of “Inline Value Classes” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Inline Value Classes”?

Wrap without overhead. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Inline Value Classes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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