0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

val vs var

Immutable and mutable bindings.

val vs var is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two Ways to Name Values

In Scala, you bind a name to a value using either val or var.

val creates an immutable binding: once assigned, it can never point to another value. var creates a mutable binding that you can reassign later.

Choosing between them is one of the first habits a Scala backend engineer builds.

val name = "Ada"
var score = 10

Defining a val

A val is read-only. You assign it once, and the compiler guarantees it stays the same.

This makes your code easier to reason about: a val never surprises you with a changed value somewhere else.

object Main extends App {
  val pi = 3.14
  println(pi)
}

Reassigning a val Fails

Trying to reassign a val is a compile error, not a runtime crash. The compiler stops you before the program ever runs.

The snippet below would fail with reassignment to val.

val country = "Turkey"
country = "Spain" // error: reassignment to val

Defining a var

A var can be reassigned as many times as you like, as long as the new value has a compatible type.

Use var when a value genuinely needs to change over time, such as a counter in a loop.

object Main extends App {
  var count = 0
  count = count + 1
  count = count + 1
  println(count)
}

var Keeps Its Type

Even though a var can be reassigned, its type is fixed at definition.

If level starts as an Int, you cannot later store a String in it. The compiler rejects the mismatch.

var level = 1
level = 5        // ok, still an Int
level = "high"  // error: type mismatch

Prefer val by Default

Idiomatic Scala favors val almost everywhere. Immutability avoids whole classes of bugs and makes concurrent code safer.

  • Reach for val first.
  • Switch to var only when you truly need mutation.

Rewriting var as val

Many loops that look like they need a var can be expressed with a val and an expression instead.

Here a sum is computed with a range and sum, no mutation required.

object Main extends App {
  val total = (1 to 5).sum
  println(total)
}

Scope of a Binding

Both val and var live inside the block { ... } where they are defined.

A binding declared inside a block is not visible outside of it. This keeps names tidy and prevents accidental reuse.

object Main extends App {
  val outer = 1
  {
    val inner = 2
    println(outer + inner)
  }
  // inner is not visible here
}

Lazy val

A lazy val is computed only the first time it is used, then cached.

This is handy for expensive values you might not always need. The body runs at most once.

object Main extends App {
  lazy val greeting = { println("computing"); "Hi" }
  println("before")
  println(greeting)
  println(greeting)
}

val with Explicit Type

You can annotate a binding's type after a colon. This documents intent and can widen the type on purpose.

Here amount is declared as a Double even though 10 looks like an Int.

val amount: Double = 10
val label: String = "price"

val vs var Side by Side

The two behave the same until you try to reassign.

  • val x: assign once, fixed forever.
  • var y: assign and reassign freely.

The program below increments only the var.

object Main extends App {
  val base = 100
  var bonus = 0
  bonus = bonus + 25
  println(base + bonus)
}

Quick Check

Test your understanding of val and var.

Recap

You learned the two ways to bind names in Scala.

  • val is immutable and is the default choice.
  • var is mutable and reserved for values that must change.
  • Reassigning a val is a compile error.
  • Types are fixed for both; lazy val defers computation.

Frequently asked questions

Is the “val vs var” lesson free?

Yes — the full text of “val vs var” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “val vs var”?

Immutable and mutable bindings. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “val vs var” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. val vs var
  2. Basic Types and Literals
  3. Type Inference
  4. Expressions over Statements
← Back to Scala for Backend Engineering & Functional Programming