Learn Kotlin by Examples
Kotlin 1.9+Kotlin is a modern, concise, and safe programming language that runs on the Java Virtual Machine (JVM). It is fully interoperable with Java and is the preferred language for Android development.
These examples cover the essential features of Kotlin, including its null safety mechanisms, concise data classes, powerful collection processing, and coroutines for asynchronous programming.
Null Safety
Eliminating null pointer exceptions with this sample code demonstrating nullable and non-nullable types, safe call operator, Elvis operator for default values, and smart casts after null checks.
Data Classes
Reducing boilerplate for data holders with this code example showing data class declaration, automatically generated equals/hashCode/toString methods, copy function for immutable updates, and destructuring declarations.
Extension Functions
Adding functionality to existing classes with this sample code demonstrating extension function declaration on String class, extension property on Int, receiver object access with this keyword, and calling extensions like member functions.
When Expression
Replacing switch statements with this code example showing when expression for value matching, type checking with is operator, range checking with in operator, and when without argument for boolean conditions.
Sealed Classes
Representing restricted hierarchies with this sample code demonstrating sealed class declaration for finite subclass sets, object and data class subclasses, exhaustive when expressions without else branch, and state pattern implementation.
Lambda Functions
Writing concise anonymous functions with this code example showing lambda syntax with curly braces, implicit it parameter for single-argument lambdas, higher-order functions accepting lambdas, and lambda as last parameter convention.
Coroutines
Asynchronous programming made simple with this sample code demonstrating coroutine launch with runBlocking, suspend functions for pausable execution, delay for non-blocking waits, and lightweight concurrency without thread blocking.

