0PricingLogin
Scala for Backend Engineering & Functional Programming · Lesson

val, var and Types

Bindings and inference.

Bindings in Scala

In Scala you give names to values using bindings. The two keywords are val and var.

A val is immutable (it cannot be reassigned), while a var is mutable (it can be reassigned).

Declaring a val

A val binds a name to a value once. Trying to reassign it is a compile error.

Prefer val by default. Immutability makes code easier to reason about.

object Main {
  def main(args: Array[String]): Unit = {
    val name = "Ada"
    println(name)
  }
}

All lessons in this course

  1. Using the REPL
  2. val, var and Types
  3. Expressions over Statements
  4. String Interpolation
← Back to Scala for Backend Engineering & Functional Programming