Scala Var Val and Def
A var is a reference variable, similar to variables in languages like Java. Different objects can be freely assigned to a var, so long as the given object has the same type that the var was declared with:
Note in the example above the type of the var was inferred by the compiler given the first value assignment.
A val is a constant reference. Thus, a new object cannot be assigned to a val that has already been assigned.
However, the object that a val points to is not constant. That object may be modified:
A def defines a method. A method cannot be re-assigned to.
In the above examples, val y and def z return the same value. However, a def is evaluated when it is called, whereas a val or var is evaluated when it is assigned. This can result in differing behavior when the definition has side effects: