Scala Comments – Single Line, Multiline, Documentation Comments

Free Scala course with real-time projects Start Now!!

In our last tutorial, we discussed Scala Pros and Cons. In this, Scala Comments Tutorial, we will study various types of comments in Scala: Single Line, Multiline, Documentation Comments with their syntax and example.

So, let’s explore Scala Comments Tutorial.

Scala Comments with Syntax and Examples

Scala Comments with Syntax and Examples

2. Define Scala Comments

Comments are entities, in your code, that the compiler/interpreter ignores. You can use them to explain code, and also to hide code details.

Do you know What is  Scala Constructor?

Types of Comments in Scala

a. Scala Single-Line Comments

When you want only one line of a comment in Scala, you can use the characters ‘//’ preceding the comment.
object Main extends App

{
    print("Hi!")
    //This is a comment, and will not print
}

Here’s the output:
Hi!

b. Scala Multiline Comments

When your comments will span more than one line, you can use a multiline comment. To declare it, we use the characters ‘/*’ and ‘*/’ around the comment.
object Main extends App

{
    print("Hi!")
    /*This is a multiline comment,
    and will not print
    kbye*/
}

The output:
Hi!

Read about  Scala Regular Expressions(Regex)

c. Documentation Comments in Scala

A documentation comment is available for quick documentation lookup, and are open for review on pressing Ctrl+Q in the IntelliJ. Your compiler uses these comments to document the source code.
We have the following syntax for creating a documentation comment:
object Main extends App

{
    print("Hi!")
    /**
    * This is a documentation comment
    * This is a demo
    */
}

Output:
Hi!
To declare such a comment, type the characters ‘/**’, and then type something, or press Enter. The IDE will put in a ‘*’ every time you press enter. To end such a comment, type ‘/’ after one of the carets(*).

So, this was all about Comments in Scala. Hope you like our explanation.

Conclusion

Hence, in this Scala Comments article, we studied Scala has three kinds of comments- single-line, multiline, and documentation comments with their examples and syntax. Still, if we miss something, share with us in the comment box.

Related Topic-  Scala Control Structures

Reference

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *