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:
Syntax
// 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
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)
}
Login to try C/C++/Java/PHP code in the editor
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
- Nesting
/* ... /* ... */ ... */and expecting it to be ignored, when Scala supports nested comments but a missing closing*/comments out the rest of the file. - Using
#or--for comments as in other languages, when Scala uses//and/* */. - 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.
objectandclassare 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: