Type Aliases & Value Classes

A type alias is just a second, friendlier name for an existing type, while a value class is a zero-overhead wrapper that turns a single value into a genuinely distinct, type-safe type.

Learn Type Aliases & Value Classes in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll learn when each helps, how @JvmInline value class stops id mix-ups, its restrictions, and how it differs from a data class .

What You'll Learn in This Lesson

1️⃣ Type Aliases: Friendlier Names

A typealias introduces an alternative name for a type. It creates no new type — the alias and the original are fully interchangeable. Its only job is to make long or repeated types read better, such as turning Map<String, Int> into a meaningful ScoreBoard .

2️⃣ Value Classes: Safety Without Overhead

A @JvmInline value class wraps a single val to make a distinct type. At runtime the compiler usually inlines it to the raw value — so no object is allocated — yet the type system treats UserId and ProductId as incompatible, catching mix-ups at compile time.

Access the wrapped value with the property name you declared ( .raw here). This is the idiomatic way to model identifiers, emails, and units.

3️⃣ Methods, Restrictions, and vs data class

Value classes can carry methods and computed (get-only) properties, just not extra stored fields. The key restriction: exactly one property, and it must be a val . When you need several fields, reach for a data class instead, which always allocates a real object.

Your turn. Fill in the ___ blanks, then run and compare.

Wrap an Int of cents and add a dollars computed property.

📋 Quick Reference — Alias vs Value Class vs Data Class

Practice quiz

Does a typealias create a brand-new type?

  • Yes, a distinct type
  • No, it is just another name for an existing type
  • Only inside a class
  • Only for generics

Answer: No, it is just another name for an existing type. A typealias is purely a readable rename; the alias and original are interchangeable.

What is the main purpose of a typealias?

  • Adding type safety
  • Allocating less memory
  • Improving readability of long or repeated types
  • Making code run faster

Answer: Improving readability of long or repeated types. Aliases shorten types like Map<String, Int> for readability, not safety.

Which annotation is required on a JVM value class?

  • @Inline
  • @Value
  • @Data
  • @JvmInline

Answer: @JvmInline. On the JVM you write @JvmInline above value class.

How many properties can a value class declare in its primary constructor?

  • Exactly one, and it must be a val
  • Any number
  • Exactly two
  • Zero

Answer: Exactly one, and it must be a val. A value class wraps exactly one immutable val property.

What is the runtime benefit of a value class in the common case?

  • It is always boxed
  • It runs on a separate thread
  • The compiler inlines it to the raw value, allocating no object
  • It caches results

Answer: The compiler inlines it to the raw value, allocating no object. Where possible the wrapper is inlined away, so no object is allocated.

What type-safety win does a value class give over a typealias?

  • It adds extra fields
  • It removes the need for a constructor
  • It makes the type nullable
  • A function taking UserId rejects a ProductId at compile time

Answer: A function taking UserId rejects a ProductId at compile time. Distinct value classes prevent mixing up ids even when both wrap an Int.

How is a value class different from a data class?

  • A value class holds one property and is usually inlined; a data class holds many and always allocates
  • They are identical
  • A data class holds exactly one property
  • A value class can have no methods

Answer: A value class holds one property and is usually inlined; a data class holds many and always allocates. Value class: one val, usually no allocation. Data class: many fields, real object.

The single property of a value class must be declared as which?

  • A var
  • A val
  • lateinit
  • An interface

Answer: A val. The wrapped property must be an immutable val.

Can a value class have methods and computed (get-only) properties?

  • No, never
  • Yes
  • Only on Android
  • Only if it is also a data class

Answer: Yes. Value classes may carry methods and get-only computed properties.

When is a value class boxed into a real object?

  • Always
  • Never
  • When used as a nullable type, in a generic, or where an interface/Any is expected
  • Only in main()

Answer: When used as a nullable type, in a generic, or where an interface/Any is expected. Certain uses force boxing, though the type safety remains.