Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Getting Started

Create a 'Hello, World' scala application

In a browser

On a computer

As result Java and Scala are installed on your computer

Scala REPL

The scala installation contains a Scala command-line playground, the REPL (Read Eval Print Loop)

On the command-line type

scala                                               

Otherwise: in intelliJ type Run -> Tools -> Scala REPL...

This will print a welcome message and give the scala prompt

Welcome to Scala 3.7.2 (24.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                  
scala> 

Hello, World

At the prompt we type the scala statements and end with Enter
The statement is evaluated and the result is printed

print ("Hello, World")
// Hello, World

IntelliJ

The other way is using an IntelliJ project

Prerequisites

  • Java (latest)
  • Scala 3
  • IntelliJ iDEA
  • Scala Plugin

New Project

In IntelliJ start a new project. File -> New -> Project...
Configure your project as below. If possible, select the latest JDK, SBT and Scala 3.

Select 'Use significant indentation syntax (Optional Braces)'

We will use the Scala 3 Pythonesque syntax.

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
@main
def main(): Unit =
  //TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
  // to see how IntelliJ IDEA suggests fixing it.
  (1 to 5).map(println)

for (i <- 1 to 5) do
  //TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
  // for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
  println(s"i = $i")


Run the application

Run the application to see if everything is working.
Click on the green arrow on line 2.
The application runs on the Terminal