Scala combines object-oriented and functional programming on the JVM. It is the language behind Apache Spark, Akka, and Kafka's Streams API. Today you learn Scala's type system, immutability, and how it interoperates with Java.
Scala compiles to JVM bytecode — the same bytecode as Java. This means: access to every Java library, deployment on any JVM, and zero-cost Java interoperability. Scala can call Java methods directly; Java can call Scala classes. The Scala compiler adds type-safe abstractions that disappear at runtime (no overhead). Scala 3 (Dotty) modernized the language with cleaner syntax and improved type inference.
val is immutable (like Java final); var is mutable. Prefer val — immutability prevents entire categories of bugs. Scala infers types: 'val x = 42' is an Int. Explicit annotation: 'val x: Int = 42'. Types: Int, Long, Double, Boolean, String, Char, Unit (void), Nothing (no value), Any (supertype of all), AnyRef (supertype of all reference types). String interpolation: s'Hello ${name}' (s-string) and f'${price}%.2f' (formatted).
Case classes are immutable value types with built-in equals, hashCode, toString, and copy. They enable exhaustive pattern matching. 'case class Point(x: Double, y: Double)' creates an immutable data class. Pattern matching: 'shape match { case Circle(r) => ... case Rectangle(w, h) => ... }'. The compiler warns if a match is not exhaustive. Sealed traits restrict which case classes can extend them — enabling total pattern matching.
// Case classes: immutable value types
sealed trait Shape
case class Circle(radius: Double) extends Shape
case class Rectangle(width: Double, height: Double) extends Shape
case class Triangle(a: Double, b: Double, c: Double) extends Shape
def area(shape: Shape): Double = shape match {
case Circle(r) => math.Pi * r * r
case Rectangle(w, h) => w * h
case Triangle(a, b, c) =>
val s = (a + b + c) / 2
math.sqrt(s * (s-a) * (s-b) * (s-c))
}
// Collections: immutable by default
val shapes: List[Shape] = List(
Circle(5.0),
Rectangle(3.0, 4.0),
Triangle(3.0, 4.0, 5.0)
)
val areas = shapes.map(area)
val total = areas.sum
println(s'Total area: ${total.formatted("%.2f")}')
// Option: null-safe optional value
def safeDivide(a: Double, b: Double): Option[Double] =
if (b == 0) None else Some(a / b)
safeDivide(10, 2).foreach(r => println(s'Result: $r'))
safeDivide(10, 0).getOrElse(0.0) // 0.0
Model an e-commerce order system with Scala case classes: Product, OrderItem, Order, and OrderStatus (sealed trait with Pending/Shipped/Delivered/Cancelled). Write functions to calculate order total, apply discounts, and transition order status with pattern matching.