Roadmap · Stage 1: Kotlin and DSA basics
What Is a Value Class in Kotlin, and When Does It Create an Object?
In short: a value class wraps one value and gives it its own type, like
String. The compiler checks the type, but at runtime Kotlin passes the plainString, so no extra object is created. It creates a real object only when the value is used as another type, likeAny, a generic type or an item in a list.
To the compiler, an email and a phone number are both String. With value classes, they become two different types.
Look at this function:
fun sendOtp(email: String, phone: String)
sendOtp(user.phone, user.email) // compiles, but the arguments are swapped
It compiles, so nothing warns you. The bug shows up later, when a user says the code never arrived.
This post covers what a value class is, what the compiler turns it into, and when it creates an object.
What is a value class in Kotlin?
A value class wraps exactly one value and gives it a new type:
@JvmInline
value class Email(val value: String) {
init {
require("@" in value) { "Not an email: $value" }
}
}
@JvmInline
value class PhoneNumber(val value: String)
fun sendOtp(email: Email, phone: PhoneNumber) { /* ... */ }
Here, Email and PhoneNumber both hold a String, but they are different types. Now the swap doesn’t compile:
sendOtp(user.phone, user.email)
// Argument type mismatch: actual type is 'PhoneNumber', but 'Email' was expected.
The init block also checks the value, so an Email without an @ fails as soon as you create it.
A few rules, checked on Kotlin 2.4.21:
- It holds exactly one
val, declared in the constructor. - On the JVM it needs the
@JvmInlineannotation. - It can have functions and an
initblock, and it can implement interfaces. It can’t extend a class. ===doesn’t compile for it. Use==.
You already use value classes: UInt, Duration and Result in the standard library, and Dp and Color in Compose.
Why not use a typealias or a normal class?
typealias Email = String is only a second name for String. A String still fits where an Email is expected, so the swap still compiles. The Kotlin docs say this directly.
A normal class, or a data class, does give you a new type. But every Email you create is one more object in memory.
A value class gives you the type check of a class, and in most places the cost of a plain String.
What does the compiler turn it into?
Let’s create an Email and a PhoneNumber, and pass them to sendOtp:
fun sendOtp(email: Email, phone: PhoneNumber) {
println("Sending OTP to ${email.value} and ${phone.value}")
}
fun main() {
val email = Email("asha@example.com")
val phone = PhoneNumber("+91 98765 43210")
sendOtp(email, phone)
}
Now, let’s see the decompiled Java (Kotlin 2.4.21, cleaned up a little):
// Inside the Email class: what Email("...") calls
public static String constructor-impl(String value) {
if (!value.contains("@")) {
throw new IllegalArgumentException("Not an email: " + value);
}
return value;
}
public static final void main() {
String email = Email.constructor-impl("asha@example.com");
String phone = PhoneNumber.constructor-impl("+91 98765 43210");
sendOtp-JkvqDOU(email, phone);
}
public static final void sendOtp-JkvqDOU(String email, String phone) {
System.out.println("Sending OTP to " + email + " and " + phone);
}
Here, we can see three things:
Email("...")doesn’t create an object. There is nonew Email(...). Kotlin calls a static function,constructor-impl. It runs yourinitcheck and returns the sameString. So theemailvariable is a plainString.- The parameters are plain strings.
sendOtptakes twoStringvalues. TheEmailandPhoneNumbertypes only exist for the compiler. - The function name changed to
sendOtp-JkvqDOU. Kotlin adds this hash so thatsendOtp(Email, PhoneNumber)and asendOtp(String, String)don’t become the same method. Java can’t call a name with a-in it, so Java code can’t call this function.
When does a value class create an object?
The Kotlin docs give one rule: a value class becomes a real object whenever it is used as another type. This is called boxing.
I checked each case in the decompiled Java:
How you use Email |
Object created |
|---|---|
| Parameter, return value, local variable | No |
| Property of another class | No |
a == b and "$email" |
No |
Email? |
No |
As Any, a generic T, or an interface |
Yes |
In a List, Set or Map |
Yes |
Passed to a lambda (Email) -> Unit that isn’t inline |
Yes |
The nullable case depends on what is inside. Email? stays a String, because a String can already be null. But if a value class wraps an Int, like value class Age(val value: Int), then Age? becomes an object. An int can’t be null, so Kotlin needs an object to hold the null. It’s the same reason Int? becomes a Java Integer (see the null safety post).
For most code, this doesn’t matter. It matters in code that runs thousands of times, like a long list or drawing on screen. There, prefer plain parameters over List<Email> or Any, and measure before you change anything.
Inline class or value class?
They are the same feature. Kotlin 1.3 added it as inline class, in Alpha. The name confused people, because inline functions are always inlined, but these classes are not: as the table shows, they sometimes become objects.
So Kotlin 1.5 renamed it to value class with @JvmInline. Old inline class code still compiles on Kotlin 2.4.21, with the warning “‘inline’ modifier is deprecated. Use ‘value’ instead.”
Frequently asked questions
Can Java code call a value class?
Not easily, because of the changed names. Add @JvmName("sendOtpJava") to a function to keep a normal name. Kotlin 2.2 also added @JvmExposeBoxed, which adds Java-friendly versions of your functions. It is still experimental.
Can a value class hold two values? No. It holds exactly one. For two values, use a normal class or a data class.
Does == create an object?
No. Kotlin compares the two String values directly. === doesn’t compile for value classes.
What should you remember?
A value class gives a plain value its own type, so the compiler catches mix-ups like a swapped email and phone number. At runtime it is usually just the plain value. It becomes an object when used as Any, a generic type, an interface, in a collection or a lambda, or as nullable when it wraps an Int or Long.
If you want to test yourself, try the value class interview questions. This post is the seventh topic in Stage 1 of my Android roadmap. The previous one is enum classes. Next: What is an object in Kotlin, and how is it different from a class?
Which two values in your app share the same type today, and what happens if someone swaps them?