Start learning free

LESSON 3 OF 414 · WELCOME TO SCALA

Comments in Scala: Single-Line and Multi-Line

Lesson Read the full explanation

TL ; DR:

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


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

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


Full lesson:

Comments are lines of code that Scala 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 Scala:


1. Single line comments:

We create single line comments by typing // and Scala 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

    SCALA · WELCOME TO SCALA

    Comments in Scala, explained

    What comments are

    A comment is text in your code that Scala ignores completely. It never runs and never affects the output. Comments exist for people: they explain what the code does or why it does it that way, for yourself next week and for anyone else who reads the file.

    Single-line comments

    A single-line comment starts with //. Scala ignores everything from the two slashes to the end of that line. The comment ends by itself at the line break, so the next line is ordinary code again:

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

    A comment can also follow code on the same line, to explain it:

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

    The code before // still runs. Only the text after it is ignored.

    Multi-line comments

    For longer notes, start a comment with /* and end it with */. Everything between the two markers is ignored, however many lines it spans, 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!
    

    Putting /* and */ around code is a quick way to switch it off while you test something else. This is called commenting out code.

    Scala allows nested comments

    Scala lets multi-line comments nest, so a /* ... */ comment can contain another one:

    /* outer /* inner */ still comment */
    println("after nested")
    
    after nested
    

    The comment ends only at the */ that matches the first /*. This makes it safe to comment out a block of code that already contains a multi-line comment.

    Common mistakes

    • Using the wrong marker. A line starting with # is not a comment in Scala; the compiler rejects it with Illegal start of statement. Scala comments begin with // or /*.
    • Forgetting to close /*. An opened multi-line comment with no */ swallows the rest of the file, and the compiler reports unclosed comment.
    • Using println instead of a comment. println("note") prints the note; it doesn't hide it. A note for readers belongs in a comment.
    • Comments that repeat the code. // print Hello above println("Hello") adds nothing. Good comments say why something is done or what a section is for.

    The exercise

    The exercise starts with five println lines that tell a short story. The task is to document the code by adding a single-line comment, Story of Andy, on line 1, above the existing lines. The comment must start with // and must not use println, because the goal is for Scala to ignore the text, not print it:

    // Story of Andy
    
    println("My name is Andy")
    println("I started coding at 15 years old")
    println("It was love at first sight")
    println("I got the Bronze Medal at Central European Olympiad in Informatics")
    // ... the fifth println line stays as it was
    

    The fifth println line, which is not repeated here, stays exactly as it was in the editor. Run the program and the console shows the five story lines, one per println; the comment line doesn't appear. The blank line after the comment is ignored too, so it makes no difference to the output whether you keep it.

    Key takeaways

    • // comments out the rest of the line.
    • /* ... */ comments out everything between the markers, across lines, and can nest in Scala.
    • Comments never run and never change the output.
    • Use them to explain why, not to repeat what the code already says.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free