Your first Kotlin
What Kotlin is, the main function every program begins at, println and print, and output you can read one line at a time.
A modern language that runs almost anywhere
Kotlin runs on the Java virtual machine, in the browser, and on phones. The same source can be compiled for each of those places, which is why it appears in Android apps, server code and browser bundles. It has a reputation for being strict while you write and quiet once it runs, which is the deal you want from a language with years of programs ahead of it.
The editor on this page sends your program to a real Kotlin compiler and shows what it printed. There is nowhere for a program to read input from here, so every example writes its own data and prints its own answer. That keeps each lesson predictable, and it is the only limitation this course asks you to accept.
The entry point
Every program in this course starts at a function called main. The word fun introduces a function, main is its name, the empty parentheses hold its inputs — none, at the start — and the braces hold the lines that run when the program starts. The runtime looks for that exact name and signature; a file with no main compiles but has no place to begin.
Kotlin does not care how you indent. Your reader does. Four spaces per level is the habit this course uses, and the compiler only needs the braces. Indentation is how a person sees which lines belong together, so a file that is consistent is a file whose structure can be checked at a glance.
A file is read as a whole
A Kotlin file is not a transcript of keystrokes. The compiler reads every declaration before it runs anything, so a function written below main can still be called from above it. That is why the examples can put helpers after the code that uses them, and why moving a function around never changes what the program does.
The same pass checks names and types. A misspelled name, a missing argument or a value of the wrong type is reported before the first line prints, not in the middle of a run. The message names a line and a column, and the column often points at the exact word the compiler could not accept — read it before you change anything.
Printing a line
println shows one value and moves to the next line. print shows a value and stays where it is, so whatever you print next continues on the same line. The value can be text in double quotes, a number, or an arithmetic expression the compiler works out before printing. Mixing the two is how a heading and its value end up on one line without any string concatenation.
A comment starts with two slashes and runs to the end of the line. The compiler reads it and throws it away, so a comment is for the next person reading the file — often you, a week later. A comment that repeats the code word for word ages badly; one that says why a line exists keeps its value even after the line changes.
One line at a time
fun main() {
println("A line")
print("no ")
print("line break")
println()
// This line is a comment.
println(3 * 4)
}Run it, then make it yours
Press Run and read the output one line at a time. Then change the name, add a line that prints a sum of your own, and move one line above another so you can watch the order follow.
What happens when you press Run
The file you edit is source text, and the Kotlin compiler reads all of it before a single line runs. It checks that every name exists, that the types line up, and that main is shaped the way the runtime expects. Only when the whole file passes does it produce bytecode for the Java virtual machine. That bytecode is what actually runs, which is why the compiler can be strict without slowing the program down.
That order explains the two kinds of failure you will meet. A compile error stops the program before main is called, and the message names the line the compiler could not make sense of. A runtime error happens later, while a line is executing, and the lines before it have already printed. Read the output from the top: the first line that is missing tells you how far the program got.
Three habits prevent most first-day errors. Close every quote before you move on, because one missing quote turns the rest of the file into text. Count the braces around main, since a missing one makes the compiler complain at the end of the file rather than where you forgot it. And keep every instruction inside main or inside a function that main calls, because a loose line at the top level is not a statement the compiler will accept.
Try these next
- Print a two-line address with three
printlncalls, then rewrite it withprintand one newline. - Add a comment above each printed line saying what you expect it to show.
- Break a quote on purpose, read the compiler message, then repair it.
A ticket printed in pieces
Run it, then change the event name and the ticket count. Add a line that prints the booking fee, and use `print` once more so two values share a line.
Read the output pane from the top. When a line you expected is missing, look at the line above it in your program: a missing quote turns the rest of the file into text, and the compiler says so on a line of its own.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
Which function does a Kotlin program start at?
