🎓 A1Learner
🔍
0 of 50 lessons completed

Scala Get Started

A Scala program is a set of instructions saved in a file that the computer follows in order. The @main line marks the starting point where running begins.

A complete program prints one line

Every program in this course uses the same @main starting line. The indented line under it is the instruction that runs.

@main def main(): Unit =
  println("My first Scala program")

A program can hold several instructions

Each instruction sits on its own indented line. The lines run from top to bottom.

@main def main(): Unit =
  println("Step one")
  println("Step two")

A program can calculate as it runs

Instructions can work out number results. Each result prints on its own line.

@main def main(): Unit =
  println(20 + 5)
  println(20 - 5)

Example

@main def main(): Unit =
  println("Starting")
  println(2 + 3)
  println("Finished")

Key points

  • File: a program is saved as text in a file before it can run.
  • Starting point: @main def main marks the first code that runs.
  • Unit: the word Unit in the @main line means the program gives back no result value.
  • Indentation: lines under @main are indented by two spaces to show they belong to it.
  • Compile: compiling turns the saved code into a form the computer can run.
  • Run: running a compiled program follows its instructions and shows its output.

In this example, the program starts at the @main line. It prints the starting text, then prints the result of 2 + 3, then prints the closing text.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Scastie

Exercise

What does the @main line do in a Scala program?