Questions
Recommended Resources
Answer
An inline function instructs the compiler to copy the function's code and paste it directly into the call site, instead of creating a standard function call object.
Why use it?
- Performance: Reduces overhead of function calls and object allocation, especially for Higher-Order Functions (functions that take lambdas).
- Without `inline`, every lambda passed to a function creates an anonymous class object in Java bytecode, which costs memory.
Example
```kotlin inline fun executeOp(op: () -> Unit) { println("Start") op() println("End") }
// Usage executeOp { println("Working") } ```
Compiler output (conceptually): ```kotlin println("Start") println("Working") // The lambda body is pasted here println("End") ```
Caveat
Don't inline large functions, as it increases the bytecode size of your application.
Want to go deeper?
Read our full guides and blog posts on Kotlin and related Android topics.
1:1 Mentorship
Get personalized guidance from a Google Developer Expert. Accelerate your career with dedicated support.
Help fellow developers prepare for interviews
Sharing helps the Android community grow 💚