What Scala is
Scala is a language for the Java platform: it compiles to the bytecode a Java compiler writes, so a Scala class is an ordinary JVM class, a Java library needs no adapter, and a Java program can call a Scala one without knowing it is Scala. The language was designed by Martin Odersky and appeared in 2004; Scala 3, the version this course teaches, arrived in 2021 with a new metaprogramming model, enum, given and significant indentation. The name is short for "scalable language": the same syntax carries a three-line script and a distributed data engine.
A first program is one entry point and one line, and the collections it uses are immutable by default:
hello.scala
@main def hello(): Unit =
val languages = List("Scala", "Java", "Kotlin")
languages.foreach(name => println(s"Hello, $name"))
scala-cli run --server=false hello.scala
Hello, Scala
Hello, Java
Hello, Kotlin
val declares a value that never changes, List is a linked list of the standard library, and foreach takes a function. Nothing here needs a class, a type annotation or a semicolon.
Java underneath
The type system is the part Java does not have. A case class is a value with a name, an equality, a toString and a copy written by the compiler, and a pattern such as point.x reads a field without a getter:
points.scala
case class Point(x: Int, y: Int)
@main def points(): Unit =
val points = List(Point(1, 2), Point(3, 4))
val moved = points.map(point => point.copy(x = point.x + 10))
println(moved)
println(moved.map(_.x).sum)
scala-cli run --server=false points.scala
List(Point(11,2), Point(13,4))
24
The Java library is there under the Scala names. java.time, java.nio, java.net.http and the rest of the JDK are imported as they are, which is why a Scala program can use every library the platform has:
dates.scala
import java.time.LocalDate
@main def dates(): Unit =
val date = LocalDate.of(2026, 11, 26)
println(date.plusDays(5))
println(date.getDayOfWeek)
println(date.getYear)
scala-cli run --server=false dates.scala
2026-12-01
THURSDAY
2026
Where Scala is used
The language lives where a JVM program has to be correct and fast at the same time. Apache Spark, the data engine of most batch pipelines, is written in Scala and exposes its API in it; Apache Kafka, the log that event-driven systems are built on, is Scala too. Akka and its successor Pekko implement the actor model in Scala, Play and http4s serve web applications, and the build tool sbt configures a project with a Scala file (build.sbt). The Java platform underneath means a team can add Scala to a Java codebase one module at a time.
Scala is also a teaching language: its type system carries ideas such as type classes, variance and immutability that other languages spell differently. The later chapters of this course take them one at a time, and every lesson is a whole program whose output is copied from a real run of it.
What Scala adds
- Types that carry meaning. Option says a value may be absent, Either says which of two things happened, and an enum lists the cases. The compiler checks that every case is handled.
- Immutability by default. val and the immutable collections make a value safe to share, and a var is a visible decision rather than the default.
- Functions as values. A function can be stored, passed and returned, so map, filter and fold replace most loops.
- Pattern matching. A value is taken apart by its shape, which is how a case class, an enum and a sealed hierarchy are read.
- Given and using. A dependency is passed implicitly but declared in the type, so a type class such as Ordering needs no wiring at the call site.
- Both styles. Objects, traits and inheritance are there for a program that wants them; a function that returns a value is there for the rest.
Where to go next
The next lesson installs the toolchain and reads the version lines; Install Scala links one page per system. The first program then writes a whole program, and scalac and Java takes the same code to a class file that java runs.
Related
- Install Scala and check the version: the JDK, scala-cli and the version lines.
- Java: the platform and the standard library Scala uses.
- Groovy: another JVM language, dynamic where Scala is typed.
- JUnit 5: the test platform the Scala test frameworks run on.
Article author: Arthur Isaev