Skip to content
Lesson 1 of 19

Step 1 of 5 · Reading · ~3 min

Learn

Getting Started

Hello, Kotlin

Every Kotlin program starts in the same place: a top-level function named main. No class wrapper, no public static void, no rule about what the file is called. A file containing one function is a complete, runnable program.

fun main() {
    println("Hello, World!")
    println("Ship That Code")
}

//> Hello, World!
//> Ship That Code

Why is there no class?

Kotlin compiles to JVM bytecode, and the JVM really does need a class — so the compiler quietly makes one for you (a file called app.kt becomes a class called AppKt). The point is that you never write the wrapper yourself. That is Kotlin's governing idea in miniature: boilerplate the compiler can work out is boilerplate you should not be typing.

The same idea explains the missing semicolons. A statement ends at the end of the line. You may write ; to put two statements on one line, but almost nobody does.

Two forms of main are legal:

fun main() { }                      // the one you will use here
fun main(args: Array<String>) { }   // when you want command-line arguments

Exercises in this course feed data in through standard input rather than arguments, so the no-parameter form is all you need.

print or println?

These two are behind more failed submissions than any other pair of functions.

CallWhat it writesWhere the cursor ends up
println("hi")hi followed by a newlinestart of the next line
print("hi")hi and nothing elseright after the i
fun main() {
    print("Ship")
    print("That")
    println("Code")
    println("done")
}

//> ShipThatCode
//> done

print adds nothing at all — not a space, not a separator. The first line only ends because of the println at the end of it.

The idiom and the trap, side by side

println is the one to reach for, and the example above is the reason: print leaves the cursor exactly where it is, so whatever prints next lands on the same line. A program that should print two lines prints one, and not a single character of the text is wrong.

This first exercise prints only one line, and the grader trims whitespace off the very end before comparing — so here print happens to slip through. Do not learn it that way. From the moment an exercise prints two things, print is the bug.

✓ Correct:

println("Hello, Kotlin!")

✗ Wrong — decoration nobody asked for:

println("The greeting is: Hello, Kotlin!")

✗ Wrong — and by far the most common failure on this exercise. Text has to be inside double quotes; bare words are read as names:

println(Hello, Kotlin!)
// error: expecting ','

Single quotes do not rescue it. 'K' is a Char, which holds exactly one character, so 'Hello, Kotlin!' fails with too many characters in a character literal.

Comments

// a single-line comment
/* a block comment,
   which may span several lines */

The compiler ignores both. That is why a starter file made almost entirely of // TODO comments still compiles and runs — it just does not do anything useful yet. Every exercise in this course begins in exactly that state.

Your exercise

First Print hands you a main containing a single comment. Replace the comment with one statement that prints Hello, Kotlin!.

The grader runs your program with no input at all and compares the output against Hello, Kotlin!, ignoring whitespace at the very end. Nothing else is ignored. Almost every failure here is a quoting mistake rather than a logic one — the text belongs inside double quotes — and after that it is spelling: a lowercase k, a missing !, or a stray space inside the quotes each change the text and fail.

Up nextval and varGetting Started

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…