🐦 Kotlin
Advanced
What is a value class (inline class) in Kotlin?
Answer
A value class (declared with @JvmInline value class) wraps exactly one property and is represented as the wrapped type at runtime — no wrapper object is created. This gives you type safety without memory overhead. Example: @JvmInline value class UserId(val id: Long). You cannot accidentally pass a ProductId where a UserId is expected, even though both wrap a Long. At the JVM level, the compiler inlines the underlying type wherever possible, so UserId is just a Long at runtime. Value classes are ideal for domain modeling where you want strong types without allocation cost.