Start learning free

LESSON 3 OF 348 · WELCOME TO SCALA

Comments in Scala in Scala

Lesson

TL ; DR:

  • Comments are lines of code that Scala will intentionally ignore (not execute). They don't do anything.


  • You add a single line comment in Scala by typing // followed by any text:

    // This is a comment!
    println("This is not!")
    


Full lesson:

Comments are lines of code that Scala will intentionally ignore. They don't do anything.

They're just used to create notes for yourself and others about what the code does.

There are two types of code comments in Scala:


1. Single line comments:

We create single line comments by typing // and Scala will ignore (not execute) any text between // and the end of the line.

Single line comments automatically end at the next line of code:

// Next line will greet the user:
println("Hello user!")

Output of this code:

Hello user!

We can also use single line comments at the end of a line to explain the code:

println("Hello user!") // This line greets the user
println("This is not a comment!")

Output of this code:

Hello user!
This is not a comment!

2. Multi-line comments

We can also comment multiple lines of code using multi-line comments.

We type /* to begin the comment and type */ to end the comment:

/* This is a 
multi-line comment
println("This will not run")
Next line will greet the user:
*/

println("Hello user!")

Output of this code:

Hello user!

Assignment
Follow the Coding Tutorial and let's write some comments!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    Comments in Scala

    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free