lateinit var vs. val by lazy in Android

Tests your grasp of Kotlin's non-nullable property initialization. Define `lateinit` (mutable, framework-init) vs. `lazy` (immutable, deferred-init). Use `lateinit` for Views in `onCreate` and `lazy` for expensive objects. Red flag: confusing their mutability.
This tests your grasp of Kotlin's non-nullable property initialization strategies and their trade-offs in mutability, thread safety, and performance in Android. A strong answer defines `lateinit` as a mutable promise for external initialization (like in `onCreate`) and `lazy` as an immutable property whose expensive creation is deferred until first access. Give clear examples: `lateinit` for a RecyclerView adapter and `lazy` for a heavy ViewModel. A common red flag is suggesting `lazy` for a property that needs to be reassigned.
Read the original → kotlinlang.org
- #kotlin
- #android
- #memory management
- #lifecycle
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.