Scala from the ground up

Your first Scala program

What Scala is, the object with a main body every program starts from, and how to print a line of text on the screen.

Part 1 of 12

Beginner13 min readBack to Courses

Scala, and what it is for

Scala is a language that runs on the same virtual machine as Java, so it can use every library written for that world while letting you write far less ceremony around it. It mixes two ideas in one language: objects with state, like the classes you may know, and functions treated as ordinary values that you can store and pass around.

The name is short for scalable language, and the point is that the same language works for a ten-line script and for a service handling millions of requests. Big data tools, streaming pipelines and backend services are where you will meet it most, and reading it means reading code that is short and dense by design.

An object with a body

Every program here is one object with a body. object Main extends App { ... } says: make me the single thing called Main, and take everything inside the braces as the instructions to run. That is the whole file. There is no separate method to name and no class to build first.

The name Main matters because the file is called Main.scala. Whatever the file is called, the runner looks for a program that starts there, so the short form above is the safest shape to begin with. Everything you want to happen goes between the two braces, one step per line, top to bottom.

Printing, and joining text to numbers

println(...) prints what you give it and moves to the next line. Text goes inside double quotes. A number printed on its own comes out as digits, and to put text and a number on one line you join them with a plus sign: "scala ok " + 6 * 7 prints the words followed by the answer.

The plus sign works because text plus anything becomes text. Multiplication happens before the joining, so "items " + 6 * 7 prints the product rather than a row of numerals glued together. Nothing here reaches the screen until you press Run, and a short pause on the first Run is the compiler at work on the other side of the network.

From a file to a running program

Pressing Run sends the text of Main.scala to a Scala compiler on the runner. The compiler reads the whole file first, turns it into bytecode for the Java virtual machine, and only then starts it. That is why the first run takes a moment: nothing on the screen can happen until the compiler has accepted every line.

The object is a single instance with a name, and extends App gives it the main method the virtual machine looks for. Inside the braces each line is an expression evaluated in order. println is a method call that writes to standard output, which the runner collects and sends back to the pane under the editor.

A compile error stops the program before its first line runs, and the message points at a line and column. That position is a hint rather than a verdict: an unclosed brace near the top is often reported at the bottom of the file, because the compiler only notices at the end that something never closed.

Every line is an expression

Every line inside the braces is an expression, and the object runs them in order. val gives a name to the result of one, and a line that only prints still has a value — Unit, the type of work that produces nothing to keep. That is why a program can be read as a list of values being worked out rather than a list of instructions.

The braces are the body of the object, and indentation is a reading aid rather than a rule: the compiler does not care how far a line is indented, so the shape of the code is a courtesy to the next reader.

Three mistakes catch almost every first program: Println with a capital P (Scala is case-sensitive), text in single quotes (single quotes are for one character, so 'hello' does not compile), and a missing closing brace. The compiler reports the last one at the end of the file, not at the line that is actually short.

Try these next

  • Print three lines about yourself, using println for two of them and print for one.
  • Print 7 * 6, then print "7 * 6" in quotes, and compare what appears.
  • Add a comment above one line, then remove a closing brace and read where the compiler says the problem is.

The smallest Scala program

object Main extends App {
  // Two slashes start a comment the compiler skips.
  println("Hello, Scala!")

  // println ends the line; print does not.
  print("no line break yet ")
  println("and now the line ends")

  // Text joined to a number.
  val year = 2004
  println("Scala appeared in " + year)
  println("scala ok " + 6 * 7)
}

Run it, then make it yours

Press Run and read the lines of output. Then add a line of your own, and print a calculation by joining text to a number with a plus sign.

Output
Click "Run" to execute your code

A jar label

Run it and read the label as one block. Then change the jam, the jar count and the price, and add a line that prints the price per jar.

Output
Click "Run" to execute your code

Scala cares about capital letters. println, Main and App are written exactly like that; the compiler will not guess what you meant, so a wrong capital is an error rather than a style choice.

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What does extends App do in this file?