Comments In Scala

Comments are entities, in our code, that the interpreter/compiler ignores. We generally use them to explain the code, and also to hide code details. It means comments will not be the part of the code. It will not be executed, rather it will be used only to explain the code in detail.

In other words, The scala comments are statements which are not executed by the compiler or interpreter. The comments can be used to provide explanation or information about the variable, class, method, or any statement. This can also be used to hide program code details.

In Scala, there are three types of comments:



  1. Single – line comments.
  2. Multi – line comments.
  3. Documentation comments.

Here we are going to explain each and every type with their syntax and example:

Scala Single-Line Comments

When we need only one line of a comment in Scala that is we only want to write a single line comment then we can use the characters ‘//’ preceding the comment. These character will make the line a comment.
Syntax:

//Comments here( Text in this line only is considered as comment )

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// This is a single line comment.
  
object MainObject 
    def main(args: Array[String]) 
    
        println("Single line comment above"
    

chevron_right


Output:

Single line comment above

Scala Multiline Comments

If our comment is spanning in more than one line than we can use a multiline comment. we use characters ‘/*’ and ‘*/’ around the comment. That is we write a text in between these characters and it become a comment.
Syntax

/*Comment starts
continues
continues
.
.
.
Commnent ends*/

Example

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to show multi line comments 
  
object MainObject 
    def main(args: Array[String]) 
    
        println("Multi line comments below")     
    
      
    /*Comment line 1 
     Comment line 2 
     Comment line 3*/

chevron_right


Output

Multi line comments below

Documentation Comments in Scala

A documentation comment is used for quick documentation lookup. These comments are used to document the source code by the compiler. We have the following syntax for creating a documentation comment:
Syntax

/**Comment start
*
*tags are used in order to specify a parameter
*or method or heading
*HTML tags can also be used 
*such as <h1>
*
*comment ends*/

Example

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to show Documentation comments
  
object MainOb 
    def main(args: Array[String])   
    
        println("Documentation comments below")     
    
      
    /** 
    * This is geek for geeks 
    * geeks coders
    
    */
}

chevron_right


Output

Documentation comments below

For the declaration of such a comment, type the characters ‘/**’, and then type something, or we can press. So every time when we press the enter, the IDE will be put in a ‘*’. To end a comment, type ‘/’ after one of the carets(*).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.