Scala Comments
A comment is a note inside code that the computer ignores when the program runs. Comments explain code to the people who read it.
A single-line comment does not print
The // note sits above the printing line. Only the println line runs.
@main def main(): Unit =
// This note explains the line below
println("Comments do not print")A block comment can cover several lines
The /* marker starts the note and */ ends it. Lines inside the note are skipped.
@main def main(): Unit =
/* This note covers
two lines of text */
println("The block comment is skipped")Commented-out code does not run
Putting // in front of a println turns it into a note. That line prints nothing.
@main def main(): Unit =
println("This line runs")
// println("This line does not run")Example
@main def main(): Unit =
// Greeting program
println("Hello")
/* The next line
prints the closing word */
println("Done") // End of the programKey points
- Single-line comment: two slashes // start a note that ends at the end of the line.
- Block comment: /* starts a note and */ ends it, and the note can cover several lines.
- Ignored: the computer skips comments, so comments never print.
- Placement: a comment can sit on its own line or after code on the same line.
- Purpose: comments explain why code exists in plain words for readers.
In this example, the // line and the /* */ block are notes that never print. The two println lines run in order and print Hello and Done.
Note
turning a line of code into a comment stops that code from running.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Scastie
Exercise
Which line is a single-line comment in Scala?