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
- Using the REPL
- val, var and Types
- Expressions over Statements
- String Interpolation