Kotlin
225 bites tagged Kotlin — interview questions with model answers, and 60-second explainers.
Kotlin Coroutines on Android
Kotlin coroutines let Android code write asynchronous logic that reads like sequential code, suspending instead of blocking a thread. Structured concurrency ties each coroutine to a scope, so a destroyed ViewModel cancels its children automatically.
Kotlin Sealed Classes: Enums for Types
A sealed class is like an enum, but for types. It defines a closed set of subclasses, letting each one carry different data. It's perfect for modeling states like `Loading`, `Success`, and `Error`, ensuring you handle every case at compile time.
Kotlin Scope Functions: Cleaner Code, Clearer Choices
Kotlin's scope functions (`let`, `run`, `apply`) create a temporary workspace for an object, avoiding repetitive variable names. Use them for object configuration or chaining calls.
Kotlin Extension Functions: Add Methods Without Inheritance
Kotlin extension functions let you add new functionality to existing classes without inheritance, as if you were adding a new method. They're perfect for creating helpers for third-party library classes or framework types like `String`.
Kotlin Data Classes: Automatic Boilerplate for Data Holders
A Kotlin `data class` automatically generates boilerplate like `equals()` and `toString()` for classes that just hold data. Use it for model objects or DTOs where value equality matters. The footgun: its `copy()` method is shallow, sharing mutable objects.
Kotlin Inheritance: Open for Extension, Closed by Default
In Kotlin, classes are final by default. Think of inheritance as an opt-in feature you enable with the `open` keyword. Use it to create specialized versions of a base class, like a `Student` from a `Person`.
Kotlin Functions: Named, Reusable Code Blocks
A function is a named recipe for your code. You give it ingredients (parameters) and it produces a result. They are used everywhere, from calculating values to handling button clicks.
Kotlin Null Safety: Catch Nulls at Compile Time
Kotlin's type system catches null pointer errors at compile time. Variables are non-nullable by default; you must opt-in to nulls with a `?` (e.g., `String?`). The compiler then forces you to handle the null case. The main footgun is the `!!` operator.
Kotlin Variables: `val` for Constants, `var` for Variables
In Kotlin, `val` creates a read-only constant you assign once, like a fixed setting. `var` creates a mutable variable you can change later. Always prefer `val` unless you explicitly need to reassign a value to prevent accidental state changes.
Get Kotlin bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.