Scala Introduction
Scala is a statically typed language that runs on the Java Virtual Machine (JVM) and blends two styles of programming that are often treated as rivals: object-oriented programming (classes, objects, inheritance) and functional programming (immutable values, functions as data, expressions instead of statements). The name comes from "scalable language" — the idea is that the same small set of features works for a ten-line script and for a system with millions of lines.
Scala was created by Martin Odersky at EPFL in Switzerland and first released in 2004. Scala 3, released in 2021, cleaned up the syntax (optional braces, enum, given/using, extension methods) and is the version this course teaches.
What is Scala used for?
- Data engineering — Apache Spark, the most popular big-data engine, is written in Scala and has a first-class Scala API.
- Back-end services — streaming systems such as Apache Kafka started in Scala, and libraries like Akka/Pekko, http4s, ZIO and Play power high-traffic APIs.
- Compilers and tools — Scala's expressive types make it popular for writing language tooling.
- Finance and analytics — many banks and trading firms value Scala's safety and conciseness.
- The browser and native code — Scala.js compiles to JavaScript and Scala Native compiles to machine code.
Why learn Scala?
| Feature | What it means for you |
|---|---|
| Runs on the JVM | Use any Java library directly; deploy anywhere Java runs |
| Static types with inference | The compiler catches mistakes, but you rarely write the types yourself |
| Functional and OO together | Model data with case classes, transform it with map/filter/fold |
| Immutable by default | val and immutable collections make code easier to reason about |
| Pattern matching | Take data apart and branch on its shape in one readable expression |
| Powerful collections | A rich, uniform library for lists, vectors, sets and maps |
Your first Scala program
In Scala 3 the simplest program is a method marked with the @main annotation. Whatever name you give it becomes the program's entry point.
@main def run(): Unit =
println("Hello, Scala!")Hello, Scala!
println prints a value followed by a line break. Unit is Scala's "no useful result" type, similar to void in Java or C.
Note: Scala cannot be compiled inside a web page, so on this site
scalaplaygrounds are simulated: you see the saved output. Every example was run with real Scala 3.7.1 to produce that output, so it is exactly what you will get on your own machine.
Everything is an expression
In many languages if and blocks are statements that do something. In Scala almost everything is an expression that produces a value. That is why you will rarely see a return keyword: the last expression in a block is its result.
@main def run(): Unit =
val temperature = 23
val feeling = if temperature > 20 then "warm" else "chilly"
val summary =
val unit = "°C"
s"$temperature$unit feels $feeling"
println(summary)23°C feels warm
Here if … then … else returns a string, and the indented block after val summary = returns the value of its last line.
A taste of functional style
Scala's collections come with dozens of methods that take functions as arguments. The program below keeps the even numbers, squares them and adds them up — no loop variables needed.
@main def run(): Unit =
val numbers = List(1, 2, 3, 4, 5, 6)
val result = numbers.filter(n => n % 2 == 0).map(n => n * n).sum
println(s"Sum of even squares: $result")Sum of even squares: 56
Scala vs. other languages
| Scala | Java | Python | |
|---|---|---|---|
| Typing | static, inferred | static | dynamic |
| Runs on | JVM (also JS, native) | JVM | interpreter |
| Default variables | immutable val |
mutable | mutable |
| Main paradigm | functional + OO | OO | multi-paradigm |
| Null safety | Option instead of null |
Optional (optional) |
None checks |
How this course is organised
- Scala Tutorial — syntax, variables, types, strings, control flow and functions.
- Scala Collections —
List,Vector,Set,Map, tuples and the methods that transform them. - Scala OOP & FP — classes, case classes, traits, enums,
Option/Either/Try, givens and type classes. - Scala Advanced — futures, error-handling strategies, Java interop, build tools and testing.
- Practice — exercises and a small project that pulls everything together.
Tip: Copy the examples into your own editor and change them. Breaking a program on purpose and reading the compiler's error message is one of the fastest ways to learn Scala.
Test yourself
Which platform does Scala primarily run on?
- The .NET CLR
- The Java Virtual Machine
- The Python interpreter
- Only in the browser
What marks the entry point of a Scala 3 program in this course?
public static void mainfun main()@main def run(): Unitif __name__ == "__main__"
What is the value of if 3 > 2 then "yes" else "no" in Scala?
- Nothing,
ifis a statement true"yes"- A compile error