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 withIllegal 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 reportsunclosed comment. - Using
printlninstead 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 Helloaboveprintln("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.
