BeginnerLesson 1 of 16
Hello World & main – Kotlin
Your first Kotlin program, output, and comments.
A Kotlin program starts in the function main. It needs no class and no semicolons. You print a line with println.
fun main() {
println("Hello World!")
print("No line break")
println()
println("Sum: ${1 + 2}")
}Comments: // for a single line, /* ... */ for multiple lines (which can even be nested), /** ... */ for KDoc documentation. If your program needs command-line arguments, you write fun main(args: Array<String>).
// Single-line comment
/* Multi-line
/* may be nested */
*/
/** KDoc: describes the following function. */
fun main(args: Array<String>) {
println("Arguments: ${args.size}")
}TipIn the following lessons we often show only individual lines. In the Kotlin Playground, simply wrap them in
fun main() { ... }.WarningKotlin is an expression-based language: many things that are statements in Java (
if, when, try) return a value in Kotlin. You will run into this constantly in the next lessons.