Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 37 ยท Getting Started

What Is Scala?

Learn what Scala is, why it blends object-oriented and functional programming on the JVM, what Scala 3 changed, and where Scala is used in real projects.

Scala is a general-purpose programming language that runs mostly on the Java Virtual Machine (JVM). It was created by Martin Odersky and first released in 2004. The name is short for "scalable language": the same language is meant to work for a ten-line script and for a large distributed system.

What makes Scala unusual is that it treats two big ideas as equals. Every value is an object, as in Java, and functions are values too, as in Haskell or Lisp. You can write classes and traits for modelling, then use immutable data and small functions for the logic. This course teaches Scala 3, the modern version of the language, and mentions old Scala 2 syntax only when you may meet it in existing code.

Scala code is read-only on this site. Our in-browser runners cover many languages, but Scala is not one of them yet. Scala examples appear in plain code blocks, and I only show output when I am certain of it. Where a live demo helps, you will find a clearly labelled Java or Kotlin example next to it that you can run and edit. To run Scala itself, install it on your computer in a couple of minutes.

A first look

Here is a small Scala 3 program. Read it before we explain it:

SCALA
case class Book(title: String, pages: Int)

@main def library(): Unit =
  val books = List(
    Book("Dune", 612),
    Book("Emma", 474),
    Book("Ulysses", 730)
  )
  val long = books.filter(_.pages > 500).map(_.title)
  println(long)

This defines a Book type in one line, builds a list, keeps the books with more than 500 pages, and prints their titles. The result is List(Dune, Ulysses). There are no semicolons, no new, no getters and setters, and no explicit loop. You will learn every piece in the next lessons: case classes, the List and filter / map.

To see that the idea is not magic, here is the same program in Java, which you can run:

Java The same idea in Java (runnable stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Modern Java has caught up on records and streams, but Scala built these ideas into the language from the start, and goes further with pattern matching, traits, type inference and more.

The main ideas

Why people choose Scala

Object-oriented Classes, traits, objects
Functional Immutable data, functions as values
JVM Use any Java library
Static types The compiler catches mistakes
1 / 2

Let's look at the pieces one by one.

Statically typed with inference

Scala checks types at compile time, which finds a whole class of bugs before the program runs. But you rarely write types for local values. The compiler works them out:

SCALA
val count = 42          // Int
val name = "Ada"        // String
val ratio = count / 5.0 // Double

You still write types on method parameters and, usually, on public method results. That documents your code and keeps compile errors readable.

Immutable by default

Scala has two ways to declare a name: val (cannot be reassigned) and var (can be). Idiomatic code uses val almost everywhere. Immutable data is easier to reason about, safe to share between threads, and a perfect fit for the functional style. We cover this in values and variables.

Everything is an expression

In Scala, if, match and blocks produce values. There is no separate "statement vs expression" split to remember:

SCALA
val label = if score >= 50 then "pass" else "fail"

Java interoperability

Scala compiles to JVM bytecode, so you can call Java libraries directly, and Java can call Scala. That gave Scala access to a huge ecosystem on day one. Frameworks such as Apache Spark and Apache Kafka (the Kafka server itself was written in Scala for years) made Scala well known.

What changed in Scala 3

Scala 3 (code-named "Dotty") was released in 2021. It cleaned up the language without making it a different one:

Topic Scala 2 Scala 3
Blocks Braces required Braces optional: indentation works
Conditions if (x > 1) ... if x > 1 then ...
Enums Sealed trait tricks Real enum keyword
Implicits implicit for everything given / using, extension methods
Entry point object App extends App @main def
Object creation new Foo() Foo() (creator applications)

Scala 3 can still read most Scala 2 code, which is why this course sometimes mentions the older form. When you use a library, check that it publishes artifacts for Scala 3 (many also work through the 2.13 compatibility mode).

Where Scala is used

  • Data engineering. Apache Spark is written in Scala, and the Scala API is its native one.
  • Backend services. Play Framework, Akka/Pekko, http4s, ZIO and Cats Effect are popular choices for high-throughput servers.
  • Streaming and messaging. Many companies write Kafka and Flink pipelines in Scala.
  • Tooling and build. sbt, the standard Scala build tool, is itself written in Scala.
  • Learning. Scala is a favourite in university courses on functional programming.

Scala is probably not the first choice for a simple website, a quick script that already works in Python, or a team that wants a language it can learn in a weekend. It rewards investment: the learning curve is steeper than Kotlin's or Go's, but the type system lets you express a lot.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. On which platform does Scala mainly run?

    Scala compiles to JVM bytecode. Scala.js and Scala Native exist as extra targets, but the JVM is the main one.
  2. 2. Which keyword declares a name that cannot be reassigned?

    val creates an immutable binding. var creates a reassignable one, and idiomatic Scala prefers val.
  3. 3. Which statement about Scala 3 is true?

    Scala 3 supports significant indentation (optional braces). Semicolons are optional, Java interop is a core feature, and Scala is statically typed.
  4. 4. Can you run Scala examples in the editor on this site?

    Scala is not available in our runners, so Scala code is read-only and some lessons add Java or Kotlin demos.

Summary

  • Scala is a statically typed JVM language that combines object-oriented and functional programming.
  • Scala 3 adds optional braces, enum, given/using and @main.
  • Prefer val, immutable data and expressions that return values.
  • It interoperates with Java and also targets JavaScript (Scala.js) and native code (Scala Native).
  • Scala examples on this site are read-only; install Scala locally to run them.

Next: Install Scala and Run Code