Learn Kotlin, created by JetBrains and adopted as an official Android language. Covers null safety, concise syntax, and coroutine-based concurrency at a professional level.
Learn Kotlin, created by JetBrains and adopted as an official Android language. Covers null safety, concise syntax, and coroutine-based concurrency at a professional level. Rather than listing concepts, this guide is organized so you can follow the content in the order you have to make decisions on a real project.
Key perspective
Frontend / app development
Do not stop at drawing the screen: look at state, data requests, routing, accessibility, and the unit of deployment together.
To make what you read stick, it helps to capture the flow as a picture first. The two diagrams below are reference maps you can keep coming back to while learning Kotlin.
Learning flow
Rendering diagramβ¦
Architecture view
Rendering diagramβ¦
Environment setup
When you first open Kotlin, the big picture matters more than individual commands. This section starts with the problems the concepts ahead were created to solve.
Kotlin runs on the JVM, so a JDK is needed first. SDKMAN alone installs and version-manages both the JDK and the Kotlin compiler, which is especially handy when projects require different JDK/Kotlin combinations.
Here you look at Basic syntax & null safety alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
Kotlin declares variables with val (immutable)/var (mutable), and its type system itself distinguishes nullability, preventing NullPointerException at compile time.
main.ktKOTLIN
fun main() { val name: String = "Kotlin" // non-null: assigning null is a compile error var nickname: String? = null // nullable: null is allowed only with the ? // Safe call (?.) + Elvis operator (?:) val len = nickname?.length ?: 0 println("Name: $name, nickname length: $len") // let: runs the block only when not null nickname?.let { println("Nickname: $it") }}
Classes & data class
Here you look at Classes & data class alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
A data class generates equals/hashCode/toString/copy automatically. A sealed class creates a restricted hierarchy, forcing every branch of a when to be handled at compile time.
KOTLIN
data class User(val id: Int, val name: String)sealed class ApiResultdata class Success(val data: String) : ApiResult()data class Failure(val error: String) : ApiResult()object Loading : ApiResult()fun handle(result: ApiResult) = when (result) { is Success -> println("Success: ${result.data}") is Failure -> println("Failure: ${result.error}") Loading -> println("Loading...") // with a sealed class, no else is needed once every subtype is handled}
Extension functions & scope functions
Here you look at Extension functions & scope functions alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
An extension function can be called as if a new method had been added to an existing class (including classes you did not write). Scope functions such as let/run/with/apply/also look alike by name, but each differs in "how the receiver object is referenced" and "what is returned".
extensions.ktKOTLIN
// Extension function β adds what looks like a new method without touching the String classfun String.isValidEmail(): Boolean = this.matches(Regex("^[\w.-]+@[\w.-]+\.\w+$"))if ("user@example.com".isValidEmail()) { println("Valid email")}// apply β configures the object itself (this) and returns it (useful for initialization)val user = User(id = 1, name = "").apply { // this is the User here, so properties are accessed directly (assumes var, since val can only be set in the constructor)}// let β receives the object as an argument (it) and returns the lambda result (often combined with null checks)val length = nickname?.let { it.length } ?: 0// also β receives the object as an argument (it) and returns the original object (useful for side effects and logging)val list = mutableListOf(1, 2, 3).also { println("Created: $it") }
Function
Receiver reference
Return value
Mostly used for
let
it
The lambda result
Running only when not null, transforming a value
run
this
The lambda result
When both object configuration and a computed result are needed
apply
this
The object itself
Initializing/configuring an object and then using it as-is
also
it
The object itself
Side effects slotted into the main flow, such as logging
Coroutines
Here you look at Coroutines alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
Coroutines are a lightweight way of doing asynchronous work without blocking threads. Use launch for work whose result is not needed and async/await for parallel work whose result is.
KOTLIN
import kotlinx.coroutines.*suspend fun fetchUser(id: Int): String { delay(100) return "User-$id"}fun main() = runBlocking { val d1 = async { fetchUser(1) } val d2 = async { fetchUser(2) } println("${d1.await()}, ${d2.await()}") // runs in parallel}
Structured concurrency
Here you look at Structured concurrency alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
A coroutine always runs inside some CoroutineScope, and when the parent scope is cancelled, every child coroutine inside it is cancelled too. Thanks to this "structured concurrency", a single cancellation cleans up all the related asynchronous work without tracking each task separately.
Rendering diagramβ¦
structured_concurrency.ktKOTLIN
class UserViewModel : ViewModel() { fun loadUser(id: Int) { // A coroutine started in viewModelScope is cancelled automatically when the ViewModel is cleared viewModelScope.launch { val user = fetchUser(id) // if the screen goes away first updateUi(user) // this line never runs (cancelled safely) } }}
Kotlin practical design
Kotlin practical design is a point where the options diverge. Using the table to compare what each approach is for and how it differs in operation makes later decisions much easier.
With Kotlin, it is important to draw clear domain boundaries without overusing extension functions and null safety. Express state with sealed classes and keep data classes immutable.
Decision point
Question to ask
Practical guideline
Boundaries
Which parts of the Kotlin code are most likely to change?
Separate input/output, configuration, external integrations, and the core rules.
State
Where is state created and where does it go away?
Make the owner and lifecycle of state visible in the code.
Failure
What does the caller receive on failure?
Decide the timeout, fallback, and error contract first.
Kotlin operating standards
This section covers Kotlin operating standards from a practical perspective. Instead of memorizing the concept, focus on the situations in which you would reach for it.
In coroutine-based operations, make structured concurrency, the choice of Dispatcher, exception propagation (CoroutineExceptionHandler), and cancellation handling explicit.
Kotlin verification strategy
Kotlin verification strategy is a point where the options diverge. Using the table to compare what each approach is for and how it differs in operation makes later decisions much easier.
Coroutine tests should use runTest and virtual time, and tests should cover the null handling paths and every branch of sealed classes.
Quality axis
How to verify
Definition of done
Correctness
Automate the normal/failure cases.
The core scenarios pass reproducibly.
Regression prevention
When fixing a bug, leave the same case behind as a test.
The same failure is not deployed again.
Operability
Check the logs, metrics, and alerts.
There is a path for tracing the cause when a problem occurs.
Kotlin: frequently asked questions
How is Kotlin different from Java?
Kotlin runs on the JVM and interoperates with Java code in both directions, with more concise syntax and a type system that separates nullable types, which cuts NullPointerExceptions at compile time. It also offers data classes, extension functions, and coroutines.
Is Kotlin only for Android development?
No. It is widely used for server development with Spring and for multiplatform development that targets several platforms from one codebase. Because it runs on the JVM, Java libraries can be used as they are.
How are coroutines different from threads?
A coroutine is a lightweight unit of work that can suspend and resume. It does not hold a thread while waiting, so far more coroutines than threads can run at the same time. Work with long waits, such as network requests, can be written in sequence without callbacks.