โ Scala Programming learning pathScala Programming ยท Module 2 of 10
Values, variables and types
Scala prefers immutable values and infers types.
๐ Key ideas
- val creates an immutable value; var creates a variable that can change.
- Type inference: val n = 5 is an Int without writing the type.
- You can annotate types: val name: String = "Asha".
- Common types: Int, Long, Double, Boolean, String, Char, Unit.
- 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"