← Back to Scala Course | Chapter 1: Setup & Basics | Lesson 8 of 8

Comments

Comments are notes for people that Scala ignores, like a sticky note on a recipe. They explain your thinking without changing what the program does.

In this page:

  1. Comments
Syntax
scala
// single-line comment
/* multi-line
   comment */

Comments

A // starts a single-line comment in Scala, and /* ... */ wraps a multi-line comment block.

Scaladoc comments, written as /** ... */, are used to generate API documentation and can appear above classes, objects, and methods. Comments should explain non-obvious reasoning, not restate what the code already makes clear.

Note: /** ... */ (double-star) Scaladoc comments are recognized by documentation-generation tools, unlike plain /* ... */ comments.

Example: Comments

markup
object Main extends App {
  // This is a single-line comment
  val x = 10 // inline comment after code

  /* This is a
     multi-line comment block */

  /** Scaladoc comment describing this value. */
  val y = 20

  println(x + y)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Nesting /* ... /* ... */ ... */ and expecting it to be ignored, when Scala supports nested comments but a missing closing */ comments out the rest of the file.
  2. Using # or -- for comments as in other languages, when Scala uses // and /* */.
  3. Writing a /** ... */ Scaladoc comment away from the definition it documents, so it does not attach to anything.
Chapter Summary
  • Scala is set up with an installation, the REPL, and running scripts, and programs have a defined structure.
  • object and class are the basic building blocks.
  • println, print, and comments produce output and document code.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.