0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

val, var and Types

Bindings and inference.

val, var and Types is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 2 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.

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)
  }
}

Declaring a var

A var can be reassigned with =. Use it only when you genuinely need mutable state.

Notice that the new value must still match the original type.

object Main {
  def main(args: Array[String]): Unit = {
    var count = 0
    count = count + 1
    count = count + 1
    println(count)
  }
}

Type Inference

Scala usually figures out the type for you. Writing val x = 42 makes x an Int automatically.

This is called type inference: you get static typing without always writing the type.

object Main {
  def main(args: Array[String]): Unit = {
    val x = 42
    val pi = 3.14
    val flag = true
    println(x)
    println(pi)
    println(flag)
  }
}

Explicit Type Annotations

You can also state the type explicitly after a colon: val x: Int = 42.

This is useful for documentation, or to choose a wider type than the one inferred.

object Main {
  def main(args: Array[String]): Unit = {
    val age: Int = 30
    val price: Double = 9.99
    val label: String = "Sale"
    println(label + ": " + price)
  }
}

Common Numeric Types

Scala has several numeric types:

  • Int for whole numbers
  • Long for big whole numbers
  • Double for decimals
  • Float for smaller decimals

An integer literal defaults to Int; a decimal literal defaults to Double.

object Main {
  def main(args: Array[String]): Unit = {
    val whole: Int = 7
    val big: Long = 9000000000L
    val dec: Double = 2.5
    println(whole + dec)
  }
}

Boolean and Char

Boolean holds true or false. Char holds a single character written in single quotes.

These are full-fledged types you can store in vals and vars.

object Main {
  def main(args: Array[String]): Unit = {
    val isReady: Boolean = true
    val grade: Char = 'A'
    println(isReady)
    println(grade)
  }
}

String Type

String holds text in double quotes. Strings are immutable in Scala.

You can concatenate them with + and call many useful methods on them.

object Main {
  def main(args: Array[String]): Unit = {
    val first = "Grace"
    val last = "Hopper"
    val full = first + " " + last
    println(full.toUpperCase)
  }
}

Why val Over var?

Immutable values prevent a whole class of bugs: nothing else can change them out from under you.

  • Easier to reason about
  • Safer in concurrent code
  • Encourages a functional style

Reach for var only when mutation is truly required.

Type Safety in Action

Because Scala is statically typed, the compiler catches type mismatches before the program runs.

Assigning text to an Int would fail at compile time. Here is valid, type-correct code.

object Main {
  def main(args: Array[String]): Unit = {
    val quantity: Int = 3
    val unitPrice: Double = 4.50
    val total: Double = quantity * unitPrice
    println(total)
  }
}

Putting It Together

Combine immutable and mutable bindings with inference and annotations in one small program.

Notice how the compiler tracks every type for you.

object Main {
  def main(args: Array[String]): Unit = {
    val base = 100
    var running = base
    val step: Int = 25
    running = running + step
    println(running)
  }
}

Quick Check

Check your understanding of bindings and types.

Recap

You learned how Scala names values:

  • val is an immutable binding; var is mutable
  • Type inference picks the type from the value
  • You can add explicit annotations like val x: Int
  • Core types include Int, Long, Double, Boolean, Char, and String
  • Prefer val for safer, clearer code

Frequently asked questions

Is the “val, var and Types” lesson free?

Yes — the full text of “val, var and Types” 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, var and Types”?

Bindings and inference. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “val, var and Types” 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. Using the REPL
  2. val, var and Types
  3. Expressions over Statements
  4. String Interpolation
← Back to Scala for Backend Engineering & Functional Programming