WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kotlin · Expert · question 70 of 100

Discuss the impact of Kotlin’s inline classes on code performance and memory usage. What are the limitations of using inline classes?

📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

Inline classes are a feature introduced in version 1.3 of Kotlin. They allow developers to create classes that are optimized for performance by avoiding runtime boxing of values, which can improve code performance and reduce memory usage.

When using inline classes, the object instantiation and method invocation overhead is eliminated as the object is replaced with the actual value at compile-time. This means that there’s no allocation of heap memory for the objects, and it results in less garbage collection and better performance. Inline classes are particularly useful for implementing value types as they allow the creation of lightweight objects that behave similar to primitives.

Here’s an example of using an inline class:

inline class Dollars(val value: Double)

fun main() {
    val myMoney = Dollars(50.0)
    println("I have $${myMoney.value}") // will print "I have $50.0"
}

In the above example, the ‘Dollars‘ class is defined as an inline class, which means that the instance of the class will be replaced with the ‘Double‘ value at compile-time. This ensures that there’s no runtime overhead for the class usage.

However, using inline classes has some limitations. Inline classes can only have one property, which must be a type that is not nullable and does not have its own methods. Also, inline classes cannot be extended, and interfaces implemented by the inline class will not be available at runtime.

In conclusion, Kotlin’s inline classes can have a significant impact on code performance and memory usage by eliminating runtime overhead of object instantiation and method invocation. However, the limitations of inline classes should be taken into account before using them in the codebase.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kotlin interview — then scores it.
📞 Practice Kotlin — free 15 min
📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

All 100 Kotlin questions · All topics