Main Function
fun main() {
}The entry point of a Kotlin program.
Kotlin syntax quick reference: val/var, null safety, data classes, when and collections, from the official Kotlin docs. Every item below is clearly marked as a shortcut, command, or syntax reference.
fun main() {
}The entry point of a Kotlin program.
println("Hello")Prints a line.
val name = "Ada"
var count = 0val is read-only, var can change.
"Hi $name, total ${a + b}"Inserts values into a string.
fun add(a: Int, b: Int): Int = a + bDefines a function.
when (n) {
1 -> "one"
else -> "other"
}Matches a value against cases.
for (i in 1..10) {
}Loops over a range.
val nums = listOf(1, 2, 3)
val items = mutableListOf<String>()Creates read-only or mutable lists.
The entry point of a Kotlin program.
fun main() {
}Prints a line.
println("Hello")val is read-only, var can change.
val name = "Ada"
var count = 0Inserts values into a string.
"Hi $name, total ${a + b}"Defines a function.
fun add(a: Int, b: Int): Int = a + bMatches a value against cases.
when (n) {
1 -> "one"
else -> "other"
}Loops over a range.
for (i in 1..10) {
}Creates read-only or mutable lists.
val nums = listOf(1, 2, 3)
val items = mutableListOf<String>()Allows a value to be null.
var name: String? = nullHandles null safely with a default.
val len = name?.length ?: 0If returns a value.
val max = if (a > b) a else bCreates key-value pairs.
val ages = mapOf("Ada" to 36)Transforms a collection.
nums.filter { it > 0 }.map { it * 2 }Defines a class with a primary constructor.
class Dog(val name: String)Defines a class for holding data.
data class User(val name: String, val age: Int)Adds a function to an existing type.
fun String.shout() = uppercase() + "!"Handles exceptions (as an expression).
val n = try { s.toInt() } catch (e: NumberFormatException) { 0 }Declares a single instance.
object Config {
val version = 1
}Runs code on a non-null value.
name?.let { println(it) }Runs async work in a coroutine scope.
scope.launch {
val data = load()
}It covers 20 common Kotlin patterns and examples, including Main Function, Print Output, Val and Var, String Template, Define a Function.
Yes. Each reference page includes a copyable code example.
Check the linked documentation for language or runtime version requirements. Libraries, file paths, terminal quoting and environment settings can differ across systems; no blanket platform compatibility is implied.