Scala is a statically-typed, multi-paradigm programming language that runs on the JVM, combining object-oriented and functional programming capabilities into a unified syntax. Designed by Martin Odersky and first released in 2003, Scala provides type inference, immutability by default, and powerful pattern matching, making it particularly well-suited for concurrent and distributed systems. Scala 3 (released 2021) brought sweeping improvements: a redesigned type system with union, intersection, and opaque types; first-class enums; a cleaner replacement for implicits via given/using; and optional indentation syntax. A key insight: Scala's expression-oriented design means nearly everything returns a value — there are no statements, only expressions — which encourages a more functional style and eliminates entire categories of bugs common in statement-based languages.
What This Cheat Sheet Covers
This topic spans 23 focused tables and 269 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Basic Syntax and Variables
The first thing to internalize in Scala is the val/var split — immutable val is the default and var is the exception you reach for only when mutation is genuinely needed. This table also gathers the boundary types that sit at the top and bottom of Scala's unified hierarchy (Any, AnyRef, AnyVal, Unit, Nothing, Null) plus the Scala 3 conveniences — @main, top-level definitions, lazy val — that let you skip the ceremony older versions demanded.
| Keyword | Example | Description |
|---|---|---|
val x = 10 | • Immutable value — cannot be reassigned after initialization • preferred for most declarations | |
var y = 20y = 25 | • Mutable variable — can be reassigned • use sparingly when mutation is necessary | |
lazy val z = expensive() | Immutable value initialized only when first accessed — evaluation is deferred and cached | |
val name: String = "Alice" | Explicit type declaration — usually optional due to type inference | |
def helper(x: Int) = x + 1 def run() = println(helper(5)) | In Scala 3, definitions can appear outside any class or object — no wrapper required | |
def greet(name: String): Unit = println(s"Hello $name") | Marks a top-level method as program entry point — replaces object with main method |