Comments in Scala

In this tutorial, we will learn about the Scala comments, types of Scala comments with examples. Submitted by Shivang Yadav Last updated : April 02, 2023

Scala Comments

Comments are a very important part of programming. In Scala comments are Java-like. Comments are things that are readable by the programmer. They are added to the code to add an explanation about the source code. Commenting on a program to make it easier to understand by the programmer.

In Scala also comments are available. The comments in Scala are same as C++, Java and many other popular programming languages.

Types of Scala Comments

There are two types of comments that can be added to a program:

  1. Single Line Comments
  2. Multi Line Comments

1) Single Line Comments in Scala

It is the comment that has a span of an only single line. The line in which comment is started is commented.

Syntax

    // Single line code to be commented...

2) Multi Line Comments in Scala

A multiline comment can span up to many lines based on its end literal. A multiline comment starts and ends with a literal and all the code inside that literal is treated as a comment.

Syntax

    /* 
    Multiline
    Code to be commented 
    */

Example: Scala program to demonstrate single and multi line comments

object MyClass {
      def main(args: Array[String]) {
         //printing Hello world! - this is single line comment
         print("Hello world!");
         
         /* 
         This is multiline comment - 
         Here, we are printing Hi friends!
         */
         print("Hi friends!");
      }
   }

Output

Hello world!Hi friends!




All Scala Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.