Lesson 1 of 8

Hello, Kotlin

Write and run your first Kotlin program and learn val, var and type inference. Kotlin needs less setup code than Java: main does not need a and semicolons are optional.

Kotlin
// hello.kt
fun main() {
    println("Hello, World!")

    val name = "Ada"        // val = read-only (use by default)
    var age = 17            // var = mutable
    age = 18                // ok
    // name = "Bob"         // error: val cannot be reassigned

    println("I'm $name, age $age")          // string templates
    println("Next year: ${age + 1}")        // expressions in {}
}

Types are inferred: val name = "Ada" is a without saying so. You can be explicit — val name: String = "Ada" — and must be when there's nothing to infer from. Prefer val everywhere; reach for var only when reassignment is genuinely needed.

Kotlin
fun main() {
    val int: Int = 42
    val long: Long = 42L
    val double: Double = 3.14
    val bool: Boolean = true
    val char: Char = 'A'
    val text: String = "hello"

    // No implicit numeric conversions — convert explicitly:
    val d: Double = int.toDouble()

    // Multi-line strings:
    val poem = """
        Roses are red,
        code compiles blue.
    """.trimIndent()
    println(poem)
}
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.