Kotlin Comments
A comment is a note written in code for people to read. The compiler ignores every comment.
A comment above code
The note above the println explains that line. Only the greeting reaches the screen.
fun main() {
// Show a greeting
println("Hello")
}A comment after code
The note after the code explains that println line. The shown words are the only output.
fun main() {
println("Hello again") // Show another greeting
}A block comment
The block note spans two lines above the println lines. Both shown words still appear in order.
fun main() {
/* Show two short lines
Each line shows one word */
println("First")
println("Second")
}Example
fun main() {
// Show the title
println("Morning")
/* Show the next line
The step shows below this note */
println("Tea")
}Key points
- Use // to start a line comment.
- A line comment runs to the end of its line.
- Use /* to open a block comment and */ to close it.
- A comment can sit above a line of code or after code on the same line.
In this example, a line comment sits above the first println, and that println shows Morning. A block comment sits above the second println, and that println shows Tea. No comment text reaches the screen.
Note
A program with comments gives the same output as the same program without them.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Kotlin Playground
Exercise
What does the compiler do with a comment?