What comments are for
A comment is text inside your code that Kotlin deliberately ignores. It doesn't run, it doesn't print, and it doesn't change what the program does. Comments are notes for people: they say what a piece of code does, why it is written a certain way, or what is still left to do. Code that is clear to you today can be puzzling in a month, and a short note helps the next reader, which is often you.
Kotlin has two kinds of comments: single-line and multi-line.
Single-line comments
A single-line comment starts with two slashes, //. Kotlin ignores everything from the slashes to the end of that line. The comment ends by itself at the line break, and the next line is normal code again:
// Next line will greet the user:
println("Hello user!")
The output is only the greeting, because the comment prints nothing:
Hello user!
A comment can also follow code on the same line. Everything before the // still runs:
println("Hello user!") // This line greets the user
println("This is not a comment!")
Hello user!
This is not a comment!
The second line is code because it has no // before it. A comment affects only its own line.
Multi-line comments
To comment out several lines, start with /* and end with */. Everything between the two markers is ignored, however many lines it takes, including lines that look like code:
/* This is a
multi-line comment
println("This will not run")
Next line will greet the user:
*/
println("Hello user!")
Hello user!
The println("This will not run") line is inside the comment, so it never runs.
Details specific to Kotlin
- Block comments nest. A
/* ... */comment may contain another/* ... */comment, and Kotlin matches each opening marker with its own closing marker. Code that already has a block comment in it can therefore be commented out as a whole. In many other languages, the first*/would end the outer comment too early. - A comment you don't close. If you open
/*and never write*/, everything after it is part of the comment, and the compiler reportsUnclosed comment. - Documentation comments. A comment that starts with
/**is a documentation comment (KDoc). Tools use it to describe functions and classes. To the compiler it is just another comment. //inside a string is not a comment. Inside quotes, the characters are just text, soprintln("a // b")printsa // b.
Writing useful comments
- Explain why, not only what.
// Count from 1 because the first element is the headeris more useful than// loop. - Keep comments short and update them when the code changes. A comment that no longer matches the code is worse than none.
- Use comments to switch code off while testing. Putting
//in front of a line stops it from running without deleting it.
Common mistakes
- Using the wrong marker.
#is not a comment in Kotlin, and neither is--. Kotlin comments use//or/* */. - Using
printlnto write a note.println("Story of Andy")prints the text. Only//makes Kotlin ignore it. - Using a single slash.
/ Story of Andyis not a comment, and the compiler reports an error. - Thinking a comment must be on its own line. It doesn't, as the trailing comment above shows, but everything after the
//on that line is ignored.
The exercise
The lesson gives a short story about Andy, written as five println lines, with an empty line at the top. The task is to document it by adding the single-line comment Story of Andy on line 1, above the print lines.
Start the line with // and write the text after it. Don't use println, because the text is meant for readers of the code, not for the console. The finished code is:
// Story of Andy
println("My name is Andy")
println("I started coding at 15 years old")
// ...the other three println lines stay exactly as they were
The comment is the only change. Running the code prints the five story lines. The comment line does not appear in the output.
Key takeaways
- Comments are ignored by Kotlin and exist for human readers.
//comments out the rest of the line./* ... */comments out everything between the markers, over any number of lines, and block comments can nest.- Use comments to explain why the code does something, not to repeat what each line obviously does.