Step 1 of 5 · Reading · ~4 min
Learn
Getting Started
Hello, World
Every Kotlin program starts at a function called main. By the end of this lesson you can print a whole line, print part of a line, and join fixed text to text the program received.
fun main() {
println("Ship That Code")
println("Kotlin on the JVM")
}
//> Ship That Code
//> Kotlin on the JVM
The smallest program
fun main() is the entry point: execution starts on its first line and the program ends when the function does. There is no wrapper class and no public static void — Kotlin compiles to JVM bytecode, which does need a class, so the compiler quietly makes one for you. The point is that you never write the wrapper yourself.
The same idea explains the missing semicolons. A statement ends at the end of the line.
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, so the no-parameter form is all you need.
print and println
| Call | What it writes | Where the cursor ends up |
|---|---|---|
println("hi") | hi followed by a line break | start of the next line |
print("hi") | hi and nothing else | right after the i |
fun main() {
print("3 ")
print("2 ")
println("1")
println("liftoff")
}
//> 3 2 1
//> liftoff
The first three calls build one line, and only the println on "1" ends it. Change that call to print and the output becomes 3 2 1liftoff: two lines silently fused into one, and an exact-match grader rejects it. Every line you mean to be a line must end with println.
Joining text with +
+ between two pieces of text makes one longer piece of text. That is how fixed words and a value from the program end up on one line:
fun main() {
val user = "Grace"
println("Build passed for " + user)
println("Build " + "passed")
}
//> Build passed for Grace
//> Build passed
val user = "Grace" gives a piece of text a name; the next lesson covers val properly. For now the rule is: quotes mean literal text, no quotes means a name. Write "user" and you print the four letters u-s-e-r; write user and you print what it holds.
Spaces are part of the text. "Build passed for" + user prints Build passed forGrace, because no space was typed inside the quotes.
Where input comes from
Most exercises read standard input. The starter already does it, and hands you a name to work with:
val name = readLine()!!
Read that as "the program received one line of text and stored it in name". The readLine()!! part is explained in the next lessons; you do not need to change it.
What the compiler says
Text has to be inside double quotes. These are the three errors nearly every beginner meets in the first hour:
println(Hello) // error: unresolved reference: Hello
println('Hello') // error: too many characters in a character literal
Println("Hello") // error: unresolved reference: Println
Bare words are read as names, so Hello looks for something called Hello. Single quotes make a Char, which holds exactly one character. And Kotlin is case-sensitive, so Println is a different name from println.
A comment is text the compiler ignores: // to the end of the line, or /* across several lines */. A starter that is mostly comments still compiles and runs; it just does not do anything yet.
Common mistakes
printwhereprintlnwas needed. Two intended lines come out as one fused line.- Quoting a name.
"name"prints the four letters, not the text stored inname. - A missing space when joining.
"Hello," + nameprintsHello,Ada; the space has to be typed inside the quotes. - Different capitalisation or punctuation.
Hello, Ada!andhello, Adaare different strings, and the comparison is exact.
Your exercise
Read one line, a name, and print a two-line greeting built from it. The starter already reads the name; the two output lines are yours. The mistake the tests catch is gluing the lines together, or dropping a space or punctuation mark.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…