Null-safe greeting
Write a function that returns a user's nickname, or "guest" when nickname is null. Use the Elvis operator.
Languages / Knowledge library
a statically typed language for jvm and multiplatform
Kotlin helps teams write safer, shorter code than typical Java while staying fully interoperable with existing JVM ecosystems. It also supports Android, backend, and multiplatform apps from one language, reducing context switching and duplicated business logic.
2010: JetBrains started Kotlin to improve developer productivity on the JVM. 2016: Kotlin 1.0 was officially released. 2017: Google announced Kotlin as a first-class language for Android. 2020s: Kotlin Multiplatform and coroutines adoption grew across mobile and backend teams. 2024: Kotlin 2.0 promoted the new K2 compiler to default status.
Work through these exercises in order. Try the task before opening a reference.
Write a function that returns a user's nickname, or "guest" when nickname is null. Use the Elvis operator.
Given a list of Int, return the first 3 squared even numbers using filter + map + take.
Model API states with a sealed class (Loading, Success, Error) and render a message using when.
Create a shared validation function in common code and call it from platform-specific entry points.
Implement a suspend function with timeout and one retry on failure; return a typed Result.
A retail logistics firm managing inventory across mobile Android devices and web-based warehouse management systems.
The firm suffers from inconsistent stock calculation logic because separate teams maintain Java-based Android apps and JavaScript-based web services, leading to frequent inventory discrepancies.
Kotlin Multiplatform allows the team to consolidate core inventory calculation logic into a single shared module that compiles to both JVM bytecode for the backend and native code for mobile clients, ensuring identical business rules.
This diagram illustrates how shared Kotlin logic flows into different platform targets.
Contains core inventory logic
Consumes shared logic
Consumes shared logic
Data source
This function represents the shared inventory logic that is compiled once and used by both the JVM backend and the Android mobile application.
fun calculateStock(items: List<Int>): Int { // Shared logic function
return items.sum() // Consistent calculation across platforms
}The firm achieves a single source of truth for inventory math, eliminating logic drift and reducing the time required to update stock calculation rules across all platforms.
The primary tradeoff is the increased complexity of the build configuration and the need to manage platform-specific dependencies, which can be verified by inspecting the build output logs for unexpected target-specific compilation errors.