Learn Kotlin - 10 Code Examples & CST Typing Practice Test
Kotlin is a modern, statically typed programming language designed to improve developer productivity, safety, and interoperability, offering concise syntax, null-safety, coroutines, and seamless integration with the JVM, Android, and multiplatform ecosystems.
View all 10 Kotlin code examples →
Learn KOTLIN with Real Code Examples
Updated Nov 17, 2025
Code Sample Descriptions
Kotlin Theme Toggle and Counter
fun main() {
var count = 0
var isDark = false
fun updateUI() {
println("Counter: $count")
println("Theme: ${if (isDark) "Dark" else "Light"}")
}
fun increment() { count++; updateUI() }
fun decrement() { count--; updateUI() }
fun reset() { count = 0; updateUI() }
fun toggleTheme() { isDark = !isDark; updateUI() }
// Simulate some actions
updateUI()
increment()
increment()
toggleTheme()
decrement()
reset()
}
Demonstrates a simple counter with theme toggling using Kotlin, runnable in a JVM or Kotlin/JS environment.
Kotlin Basic Class Example
class Person(val name: String, var age: Int) {
fun introduce() {
println("Hi, I'm $name and I'm $age years old.")
}
}
fun main() {
val alice = Person("Alice", 25)
alice.introduce()
alice.age += 1
alice.introduce()
}
Defines a simple class with properties and methods, demonstrating object creation and usage.
Kotlin List Iteration
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 }
val even = numbers.filter { it % 2 == 0 }
println("Original: $numbers")
println("Doubled: $doubled")
println("Even: $even")
}
Shows iterating over a list and using functional operations like map and filter.
Kotlin Nullable Types Example
fun main() {
val name: String? = null
println(name?.length ?: "Name is null")
val greeting = name?.let { "Hello, $it" } ?: "Hello, Guest"
println(greeting)
}
Demonstrates handling nullable types and safe calls in Kotlin.
Kotlin Data Class Example
data class Rectangle(val width: Int, val height: Int)
fun main() {
val rect1 = Rectangle(10, 20)
val rect2 = Rectangle(10, 20)
println(rect1)
println("Equal: ${rect1 == rect2}")
}
Uses a data class to store immutable data with automatic equals, hashCode, and toString.
Kotlin When Expression
fun main() {
val x = 2
val result = when(x) {
1 -> "One"
2 -> "Two"
else -> "Other"
}
println(result)
}
Demonstrates the when expression for branching logic, similar to switch-case.
Kotlin Extension Function Example
fun String.shout() = this.uppercase() + "!"
fun main() {
val msg = "hello"
println(msg.shout())
}
Adds a custom function to an existing class using extension functions.
Kotlin Lambda Example
fun main() {
val numbers = listOf(1,2,3,4,5)
numbers.forEach { println(it * 2) }
}
Demonstrates lambdas and higher-order functions in Kotlin.
Kotlin Simple Timer Simulation
fun main() {
var time = 5
while(time >= 0) {
println("Time left: $time")
Thread.sleep(1000)
time--
}
println("Done!")
}
Simulates a countdown timer using a loop and Thread.sleep for demonstration purposes.
Kotlin Map Operations Example
fun main() {
val ages = mapOf("Alice" to 25, "Bob" to 30)
for ((name, age) in ages) {
println("$name is $age years old")
}
}
Demonstrates map creation, iteration, and access in Kotlin.
Frequently Asked Questions about Kotlin
What is Kotlin?
Kotlin is a modern, statically typed programming language designed to improve developer productivity, safety, and interoperability, offering concise syntax, null-safety, coroutines, and seamless integration with the JVM, Android, and multiplatform ecosystems.
What are the primary use cases for Kotlin?
Android application development. Backend APIs with Ktor or Spring Boot. Kotlin Multiplatform (KMP) shared logic. Desktop apps with Compose Multiplatform. Scripting and automation on JVM. Building DSLs and developer tooling
What are the strengths of Kotlin?
Highly concise and expressive. Eliminates null pointer crashes. Excellent for Android and JVM. Full Java interoperability. Modern concurrency support
What are the limitations of Kotlin?
KMP ecosystem still maturing. Slower compilation compared to Java. Requires JVM or native runtimes. Learning curve for coroutines and DSLs
How can I practice Kotlin typing speed?
CodeSpeedTest offers 10+ real Kotlin code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.