← Scala Programming learning path
Scala Programming · Module 2 of 10

Values, variables and types

Scala prefers immutable values and infers types.

📖 Key ideas

  1. val creates an immutable value; var creates a variable that can change.
  2. Type inference: val n = 5 is an Int without writing the type.
  3. You can annotate types: val name: String = "Asha".
  4. Common types: Int, Long, Double, Boolean, String, Char, Unit.
  5. String interpolation: s"Hello, $name" and f"$price%.2f".
💡 Real-life example: val is a printed certificate; var is a whiteboard you can rewrite.
valImmutable value
varMutable variable
Type inferenceCompiler works out the type
UnitType meaning "no useful value"