Kotlin & Coroutines (50 Qs)•Part 3: K2 Compiler, KMP, Advanced Metaprogramming & Optimization (25 - 35)
Value Classes (`@JvmInline value class`): Zero-allocation domain modeling.
senior LevelCode ExamplePerformance
In Simple Words:
Wrapping primitives (e.g. UserId(val value: Long)) in standard classes creates heap allocation overhead for millions of IDs:
kotlin
@JvmInline
value class UserId(val id: Long)
- Compile-Time: Provides strict type safety (cannot accidentally pass an
OrderIdinto a function expecting aUserId). - Runtime: The JVM compiler strips the wrapper and passes raw primitive
longvalues in registers with zero heap memory allocation!