Kotlin
Online Kotlin Compiler
main.kt is compiled by kotlinc 2.4 into a jar and run on Java 17, with the Kotlin standard library and the JDK. Programs that call readln() wait for you to type in the terminal while they run.
Try it aboveHow Kotlin runs here
- Versions
- Kotlin 2.4 on Java 17
- Build
kotlinc main.kt -include-runtime -d a.jar- Run
java -Xmx768m -jar a.jar- Limits per run
- 1 GB memory, 60 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
A Kotlin program with input and a data class
This main.kt reads a few cities and temperatures into data class objects, then sorts and converts them. Note the destructuring of the split line and the toString() that a data class writes for you:
data class Reading(val city: String, val celsius: Double) fun main() { print("How many cities? ") val n = readln().trim().toInt() val readings = List(n) { i -> print("City ${i + 1} (name temperature): ") val (city, temp) = readln().trim().split(" ") Reading(city, temp.toDouble()) } println("Warmest: ${readings.maxBy { it.celsius }}") for (r in readings.sortedBy { it.celsius }) { val fahrenheit = r.celsius * 9 / 5 + 32 println("${r.city.padEnd(6)} ${"%5.1f".format(r.celsius)} C = ${"%5.1f".format(fahrenheit)} F") } }
Three cities typed in; Cairo’s 28 becomes 28.0 because temp.toDouble() makes it a Double:
$ kotlinc main.kt -include-runtime -d a.jar $ java -jar a.jar How many cities? 3 City 1 (name temperature): Oslo 4.5 City 2 (name temperature): Cairo 28 City 3 (name temperature): Lima 19.5 Warmest: Reading(city=Cairo, celsius=28.0) Oslo 4.5 C = 40.1 F Lima 19.5 C = 67.1 F Cairo 28.0 C = 82.4 F
Compiling is the slow part of a Kotlin run, so there’s a pause before the first prompt.
Reading input in Kotlin
readln() reads one line as a String. readLine() does the same but returns String?, which is why older examples are full of readLine()!!. For numbers, read the line and convert it: readln().toInt() for one value, readln().split(" ").map { it.toInt() } for several on one line.
- toInt() takes the whole line or nothing. "3 4" or "42 " (with a trailing space) throws NumberFormatException, so trim() and split first.
- There’s no end of input in this terminal (no Ctrl+D), so generateSequence(::readLine) and System.`in`.bufferedReader().readText() never finish. Read a count first, or stop on an empty line.
- For big inputs, System.`in`.bufferedReader() is faster than calling readln() line by line, and building the output in a StringBuilder beats thousands of println() calls.
fun main() { val reader = System.`in`.bufferedReader() val n = reader.readLine().trim().toInt() val nums = reader.readLine().trim().split(" ").map { it.toLong() } val out = StringBuilder() for (x in nums.take(n).sorted()) out.append(x).append('\n') print(out) }
One file: main.kt
The build command is kotlinc main.kt, so only main.kt is compiled. A function or class in a second .kt file isn’t seen, and calling it fails with an unresolved reference. Keep everything in main.kt, with as many classes, objects and functions as you like at the top level.
Program arguments from Run settings arrive in fun main(args: Array<String>), split at spaces.
What isn’t available here
- Gradle, Maven and dependencies. kotlinx.coroutines, kotlinx.serialization, Ktor and Exposed are separate libraries, so they aren’t here. The Kotlin standard library and JDK 17 are.
- Android. There’s no Android SDK, so android.* imports, activities and Jetpack Compose don’t compile.
- Kotlin/JS, Kotlin/Native and Compose Multiplatform. Code is compiled for the JVM only.
- The network. java.net classes load, but runs are offline, so connections fail.
Errors you’ll see
- An unresolved reference error from kotlinc: a typo such as prinltn, a missing import, or a function that lives in another file, which isn’t compiled here.
- A type mismatch, for example passing readln() (a String) where an Int is expected. Convert with toInt() or toDouble().
- A val cannot be reassigned error: you assigned to a val. Declare it with var if it really has to change.
- “Exception in thread "main" java.lang.NumberFormatException: For input string: "3 4"” toInt() got a line with a space in it. Split the line and convert each part.
- “java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3” An index past the end of an array. Use getOrNull(i), or check against lastIndex.
- “java.lang.NullPointerException” from !!. A readLine()!! or map[key]!! met a null. Use readln(), or ?: to give a default.
If an unresolved reference or type mismatch doesn’t point at the fix, Fix with AI under the failed run explains it against your code and proposes a change as a diff.
Kotlin 2.4 in practice
- Kotlin 2.x uses the K2 compiler. Error messages are worded differently from Kotlin 1.x, so search for the error’s key phrase rather than the whole line.
- fun main() needs no class around it. The compiled entry point is MainKt, from the file name main.kt.
- Collections do most of the work: groupingBy { it }.eachCount() counts, sortedWith(compareBy({ it.a }, { it.b })) sorts on two keys, and windowed() and chunked() split lists.
- String templates take expressions: "${list.size} items" or "${"%.2f".format(x)}".
- when with a subject replaces long if/else chains and works on ranges: when (score) { in 90..100 -> "A"; else -> "B" }.
Questions
Which Kotlin version and JVM does it use?
Kotlin 2.4, compiled with kotlinc into a jar that runs on Java 17 with a 768 MB heap.
How do I read input in Kotlin online?
Call readln() for each line and convert it: readln().toInt(), or readln().split(" ").map { it.toInt() } for several numbers. Type the input in the terminal while the program runs.
Can I use kotlinx.coroutines or other libraries?
No. Only the Kotlin standard library and the JDK are on the classpath, and there’s no Gradle or network to fetch more.
Can I split my code into several files?
No. Only main.kt is compiled, so put all your classes and functions in it.
Why does my Kotlin program take a while to start?
kotlinc compiles main.kt into a jar on every run, and the compiler itself runs on the JVM. The program then starts in a second JVM.