Kotlin Tutorial
Master the modern language Google recommends for Android, backend, and multiplatform apps
Table of Contents
1What is Kotlin?
Kotlin is a modern, statically typed programming language developed by JetBrains. It runs on the Java Virtual Machine (JVM), Android, browsers (Kotlin/JS), and native platforms (Kotlin/Native). Google officially supports Kotlin as the preferred language for Android app development.
Kotlin was designed to be 100% interoperable with Java — you can call Java from Kotlin and vice versa in the same project. This made adoption easy for millions of existing Android and enterprise Java codebases. Kotlin code is typically more concise, safer (null safety built in), and expressive than equivalent Java.
Kotlin is not just an Android language — it's used for server-side (Spring, Ktor), multiplatform mobile (KMP), and data science tooling.
2Features of Kotlin
Kotlin's popularity comes from language features that reduce boilerplate and common bugs:
Null Safety
Nullable types (String?) enforced at compile time — fewer NullPointerExceptions.
Concise Syntax
Data classes, type inference, and smart casts cut verbose Java boilerplate dramatically.
Java Interop
Use existing Java libraries and gradually migrate files — no big-bang rewrite required.
Coroutines
First-class async programming with structured concurrency for Android and server apps.
Data Classes
data class User(val id: Int, val name: String) auto-generates equals, hashCode, copy.
Extension Functions
Add functions to existing classes without inheritance — great for UI helpers.
Sealed Classes
Restricted class hierarchies for exhaustive when expressions — ideal for UI state.
Multiplatform
Kotlin Multiplatform shares business logic across Android, iOS, desktop, and web.
3History & Versions
Kotlin was unveiled by JetBrains in 2011 and reached 1.0 in 2016. Google announced Kotlin as an official Android language at Google I/O 2017, with Android Studio receiving first-class Kotlin support.
| Year | Milestone | Significance |
|---|---|---|
| 2011 | JetBrains announces Kotlin | Open-source language project begins |
| 2016 | Kotlin 1.0 | Production-ready release |
| 2017 | Google I/O — Android support | Official Android language alongside Java |
| 2019 | Google: Kotlin preferred | New Android projects default to Kotlin |
| 2020+ | Kotlin 1.4 – 2.x | KMP stable, coroutines, K2 compiler, Compose |
4Kotlin vs Java
Both languages compile to JVM bytecode and work on Android. Kotlin is designed as a modern alternative while preserving Java ecosystem access.
| Aspect | Kotlin | Java |
|---|---|---|
| Null safety | Built into type system (?) | Optional annotations; NPE at runtime |
| Boilerplate | Data classes, properties, when | Getters/setters, verbose POJOs |
| Functions | Top-level functions, extensions | Must live inside classes (pre-Java 21) |
| Async | Coroutines (suspend functions) | Threads, CompletableFuture, RxJava |
| Android default | Google recommended since 2019 | Legacy codebases still common |
| Interop | Calls Java seamlessly | Calls Kotlin (mind nullable types) |
5Where Kotlin is Used
Android Apps
Google apps, most new Play Store apps, Jetpack Compose UI, Room, ViewModel.
Backend Services
Spring Boot, Ktor, Micronaut — REST APIs and microservices on JVM.
Kotlin Multiplatform
Share networking, models, and business logic between Android and iOS.
Desktop (Compose)
Compose Multiplatform for cross-platform desktop UIs with shared Kotlin code.
Data & Scripting
Kotlin notebooks, Gradle build scripts (.gradle.kts), automation.
Enterprise
Netflix, Pinterest, Uber, and many banks use Kotlin in production systems.
6Kotlin Platforms
Kotlin is multiplatform by design — the same language targets different runtimes:
JVM Kotlin on JVM
Default target. Compiles to bytecode compatible with Java. Used for Android and backend.
Tools: JDK, Gradle, Maven, Android Gradle Plugin.
JS Kotlin/JS
Web target. Compiles to JavaScript for browsers or Node.js applications.
Use case: Full-stack Kotlin with shared models.
N Kotlin/Native
Native binaries. No JVM required — useful for iOS, IoT, and performance-critical code.
Interop: C/Objective-C via cinterop.
M Multiplatform (KMP)
Share logic. Common module for business rules; expect/actual for platform APIs.
UI: Compose Multiplatform or native UI per platform.
7Tooling & Ecosystem
JetBrains and the open-source community provide excellent tooling for Kotlin development:
| Tool | Purpose |
|---|---|
| IntelliJ IDEA / Android Studio | Official IDE with Kotlin plugin, refactoring, debugger |
| Gradle (Kotlin DSL) | Build system — build.gradle.kts is Kotlin |
| kotlinc | Command-line Kotlin compiler |
| kotlinlang.org | Official docs, Koans (interactive exercises), API reference |
| Jetpack libraries | Android KTX extensions, Compose, Room, Navigation |
| Ktor / Spring | Popular frameworks for server-side Kotlin |
8Hello World
Every Kotlin program needs an entry point. On JVM, use the main function:
Standalone Kotlin (JVM)
Android Activity (Kotlin)
Run from command line
9Summary Cheatsheet
| Topic | Key Takeaway |
|---|---|
| What is Kotlin? | Modern JVM language by JetBrains; official for Android |
| Core Features | Null safety, conciseness, coroutines, data classes |
| vs Java | Less boilerplate; full Java interop; preferred for new Android |
| Platforms | JVM, JS, Native, Multiplatform |
| Tooling | IntelliJ, Android Studio, Gradle KTS, kotlinlang.org |
| Entry point | fun main() for CLI; Activity for Android |
| Learning path | Basics → OOP → Collections → Coroutines → Android/Compose |
10Next Steps for Learning
Complete Setup & Installation — install JDK, IntelliJ or Android Studio, and verify kotlinc.
Learn Kotlin Basics — variables, types, strings, and operators.
Master Null Safety — one of Kotlin's biggest advantages over Java.
Study OOP and Data Classes for real-world modeling.
Explore Coroutines for async Android and backend code.
Build apps with Android Development and Jetpack Compose.
Kotlin MCQ Practice
10 Basic MCQs 10 Advanced MCQs10 Basic Kotlin MCQs
Who developed the Kotlin programming language?
What runtime does Kotlin primarily target for Android and backend development?
Which symbol marks a nullable type in Kotlin?
? to a type (e.g., String?) tells the compiler the value may be null — a core null-safety feature.What is the standard entry point for a standalone Kotlin JVM program?
fun main() as the program entry point on the JVM, replacing Java's verbose main method signature.Which company officially supports Kotlin as the preferred language for Android?
Kotlin is designed to be 100% interoperable with which language?
Which Gradle build file format uses Kotlin syntax?
.gradle.kts files, letting you write type-safe build scripts in Kotlin instead of Groovy.What does Kotlin compile to when targeting the JVM?
kotlinc) produces bytecode that runs on any JVM, same as Java.Which IDE is most commonly used for Kotlin development?
When did Kotlin reach its first production-ready release (1.0)?
10 Advanced Kotlin MCQs
What is Kotlin Multiplatform (KMP) primarily designed for?
What does Kotlin/Native compile to?
What does a Kotlin data class automatically generate?
data class User(val id: Int, val name: String) auto-generates boilerplate methods that would require dozens of lines in Java.What are Kotlin coroutines primarily used for?
suspend functions and structured concurrency for network calls, database I/O, and background work on Android and Ktor servers.What is the main purpose of sealed classes in Kotlin?
when.What do extension functions allow you to do?
fun String.isEmail(): Boolean add utility methods to types you don't own — common in Android KTX libraries.What does Kotlin/JS compile Kotlin source code into?
What does the Elvis operator (?:) do in Kotlin?
val name = user?.name ?: "Guest" — if user?.name is null, the default "Guest" is used.In Kotlin Multiplatform, what are expect and actual declarations used for?
expect in common code declares an API; each platform provides an actual implementation (e.g., different file system APIs on Android vs iOS).Which backend frameworks are commonly used with Kotlin on the JVM?
15 Interview Questions & Answers
Easy Medium HardString?) and non-nullable (String) types at compile time. The compiler prevents assigning null to non-nullable types, reducing NullPointerExceptions significantly.val and var?easyval declares an immutable reference (read-only — like Java final). var declares a mutable reference that can be reassigned. Both support type inference.equals(), hashCode(), toString(), copy(), and component functions from properties declared in the primary constructor.@JvmOverloads).suspend functions allow sequential-looking async code for network, database, and background tasks without blocking threads or nested callbacks.== and === in Kotlin?medium== checks structural equality (calls equals()). === checks referential equality — whether two references point to the same object instance.when branches checked at compile time.fun View.show() { visibility = View.VISIBLE } — widely used in Android KTX.expect/actual for platform-specific APIs while keeping UI native or using Compose Multiplatform.?.) and Elvis operator (?:).medium?. safely accesses a member only if the receiver is non-null (returns null otherwise). ?: provides a default when the left expression is null: user?.email ?: "unknown@example.com".object, companion object, and class?hardclass is instantiated with constructors. An object is a singleton — one instance created by the runtime. A companion object is a singleton inside a class, similar to Java static members but is a real object with its own name.try/catch/finally, throw, and custom exceptions similarly, but the compiler doesn't force handling of checked exceptions.