Kotlin Fundamentals Flashcards
Kotlin — Fundamentals Flashcards
30 interactive flashcards covering core Kotlin concepts from null safety to inline functions. Press Space to flip, rate 1-4.
Additional Flashcard Topics
Null Safety:
String?is nullable;Stringis non-nullable. The compiler enforces null checks:?.(safe call),?:(elvis operator),!!(force unwrap — dangerous).Data Classes:
data class User(val name: String, val age: Int)auto-generatesequals,hashCode,toString,copy, and destructuring.Sealed Classes: restricted class hierarchies. All subclasses are known at compile time, enabling exhaustive
whenexpressions.Extension Functions:
fun String.isEmail() = ...— add methods to existing types without modifying them. Resolved statically (not polymorphic).Coroutines:
suspendfunctions,launch,async,withContext. Lightweight concurrency; coroutines are multiplexed onto threads by the runtime.Delegation:
by lazy { ... }for lazy initialization;by observable { ... }for change notification; custom delegates viaReadOnlyProperty/ReadWriteProperty.
Intuition
Kotlin is a modern JVM language that combines object-oriented and functional programming. Null safety is enforced at the type level — the compiler prevents null pointer exceptions by making nullable types explicit. Coroutines provide lightweight concurrency without callback hell, and inline functions eliminate the overhead of passing lambdas by inlining the lambda body at the call site. Data classes auto-generate equals, hashCode, toString, and copy methods. Kotlin is fully interoperable with Java — you can use Java libraries from Kotlin and vice versa.
Common Pitfalls
varvsvalmisuse: Usingvarwhenvalsuffices makes code harder to reason about — prefer immutability and only usevarwhen reassignment is truly needed.- Scope function confusion:
let,run,with,apply, andalsodiffer in context object (thisvsit) and return value — mixing them up leads to unreadable code. - Coroutine dispatcher selection: Running CPU-bound work on
Dispatchers.Mainfreezes the UI; running IO-bound work onDispatchers.Defaultwastes CPU threads — match the dispatcher to the work type. - Confusing
isandas:ischecks type (with smart cast);ascasts (throws ClassCastException on failure). Useas?for safe casting. - Inline function overhead:
inlineeliminates lambda allocation but can increase code size. Usenoinlinefor parameters that shouldn’t be inlined.
Cross-References
- Swift Basics: Null safety and protocol-oriented patterns compared across languages.
- Java Basics: Kotlin runs on the JVM; understanding Java helps with Kotlin interop.
- Dart Basics: Null safety and async patterns compared across languages.