Skip to main content

Online Kotlin Compiler

Main.kt
Ready

About Kotlin

Kotlin is the modern, concise language Google recommends for Android apps. It runs on the JVM and works with Java code.

How to take input in Kotlin

readLine() returns the next line as a String? (null when input runs out); !! says you expect a value. Split and convert it for numbers.

val n = readLine()!!.toInt()
val nums = readLine()!!.split(" ").map { it.toInt() }

Kotlin programs to try

Copy one into the editor above, put any input in the Input tab and press Run.

Filter and map a list

fun main() {
    val nums = listOf(1, 2, 3, 4, 5, 6)
    println(nums.filter { it % 2 == 0 }.map { it * it })
}

A data class

data class Student(val name: String, val marks: Int)

fun main() {
    val list = listOf(Student("Asha", 91), Student("Ravi", 86))
    println(list.maxBy { it.marks })
}

when expression

fun grade(marks: Int) = when {
    marks >= 90 -> "A"
    marks >= 75 -> "B"
    else -> "C"
}

fun main() {
    println(grade(readLine()!!.toInt()))
}

Input: 82

Common Kotlin errors and how to fix them

Unresolved reference
The name is misspelled or not declared or imported.
Type mismatch
A value doesn't match the expected type, for example a String where an Int is needed. Convert it with toInt().
NumberFormatException
toInt() got text that isn't a whole number. Check the Input tab.

Frequently asked questions

Is this online Kotlin compiler free?

Yes. Write and run Kotlin code in your browser for free, with nothing to install and no setup.

How do I give input to my Kotlin program?

Type it in the Input tab before pressing Run and read each line with readLine()!!.

Can I use Kotlin here to learn Android development?

You can practise Kotlin itself (syntax, collections, classes, functions). Building Android apps needs Android Studio.

Does it work on my phone?

Yes. The editor, input and output work in a mobile browser, so you can practise Kotlin without a laptop.