Start learning free

LESSON 3 OF 348 · WELCOME TO KOTLIN

Comments in Kotlin: Single-Line and Multi-Line

Lesson Read the full explanation

TL ; DR:

  • Comments are lines of code that Kotlin will intentionally ignore (not execute). They don't do anything.


  • You add a single line comment in Kotlin by typing // followed by any text:

    // This is a comment!
    println("This is not!")
    


Full lesson:

Comments are lines of code that Kotlin will intentionally ignore. They don't do anything.

They're just used to create notes for yourself and others about what the code does.

There are two types of code comments in Kotlin:


1. Single line comments:

We create single line comments by typing // and Kotlin will ignore (not execute) any text between // and the end of the line.

Single line comments automatically end at the next line of code:

// Next line will greet the user:
println("Hello user!")

Output of this code:

Hello user!

We can also use single line comments at the end of a line to explain the code:

println("Hello user!") // This line greets the user
println("This is not a comment!")

Output of this code:

Hello user!
This is not a comment!

2. Multi-line comments

We can also comment multiple lines of code using multi-line comments.

We type /* to begin the comment and type */ to end the comment:

/* This is a
multi-line comment
println("This will not run")
Next line will greet the user:
*/

println("Hello user!")

Output of this code:

Hello user!

Assignment
Follow the Coding Tutorial and let's write some comments!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · WELCOME TO KOTLIN

    Comments in Kotlin, explained

    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 reports Unclosed 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, so println("a // b") prints a // b.

    Writing useful comments

    • Explain why, not only what. // Count from 1 because the first element is the header is 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 println to write a note. println("Story of Andy") prints the text. Only // makes Kotlin ignore it.
    • Using a single slash. / Story of Andy is 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.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free