Start learning free

LESSON 1 OF 348 · WELCOME TO SCALA

Printing to the Console in Scala in Scala

Lesson

TL ; DR:

  • The console is a panel that displays important messages, like errors, for developers.


  • In Scala, we use the println() function to print something to the console. For example, this program:

    println(13)
    

    prints 13 to the console.



Full lesson:

The console is a panel that displays important messages, like errors, for developers.

Much of the work the computer does with our code is invisible to us by default. If we want to see things appear on our screen, we can print to our console directly.


println():

In Scala, we use the println() function to print something to the console. For example, this program:

println(13)

prints 13 to the console.

When you run this program, this is what you'll see in your console:

13


We refer to the data printed to the console as the output of the program.


Newline character:

You might have noticed the extra empty line after 13 in our program's output.

It's important to know that the println() function also prints an invisible newline character after the value we provide.

This newline character inserts a new line in the console. It's like pressing Enter in your text editor to move the cursor to the next line.

This means that if we use println() multiple times, the values will be printed on separate lines in the console:

println(1)
println(2)
println(3)

The ouput of this program would be:

1
2
3


Assignment
Follow the Coding Tutorial and let's print some values to the console!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    Print 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