Skip to content
AgileLogics
4 min read

How Scala Code Runs

Prerequisites: Your first program

Scala is a compiled language. When you run scala run hello.scala, several steps happen:

  1. The Scala compiler checks your code — syntax, types, and more — and reports errors before anything runs.
  2. It translates your code into JVM bytecode (.class files), the same format Java produces.
  3. The JVM loads the bytecode, compiles the busy parts to fast machine code as it runs (just-in-time compilation), and executes it.

Errors are caught before running

@main def broken(): Unit =
  val age: Int = "thirty"
  println(age)
-- [E007] Type Mismatch Error: Main.scala:2:17 ---------------------------------
2 |  val age: Int = "thirty"
  |                 ^^^^^^^^
  |                 Found:    ("thirty" : String)
  |                 Required: Int
  |
  | longer explanation available when compiling with `-explain`
1 error found

The program never ran: the compiler saw that "thirty" is a String, not an Int. Reading these messages is a skill worth building — the first line gives the error type and the location (file, line, and column), and the rest explains what was found and what was required.

Why this matters

  • Fewer runtime surprises — many mistakes that would crash a Python or JavaScript program are caught at compile time.
  • Speed — after warming up, JVM code runs very fast.
  • Start-up time — the JVM takes a moment to start, and compiling takes a few seconds. Scala CLI caches compiled code, so re-running an unchanged file is quick.

Other targets

The same Scala code can also be compiled by Scala.js into JavaScript for browsers and Node.js, or by Scala Native into a native executable that starts instantly.