We are going to start this tutorial by explaining the diferent ways in which we can name variables in Scala, by using the keyword var
or val
.
var
denotes that it is a var
iable, and as such, its value can change in different points of our program. For example:
var myNewVariable = 4
... // some time later...
myNewVariable = 40
As we can se, we are using the same variable to hold two different values 4
and 40
.
On the other hand, a val
defines a val
ue, which is something that will not change in the entire life of the application or real life. For example, the value 4 will never change, since it would make no sense that it meant something diffent.
val gravity = 9.81
If you ever try to modify a val
ue, the compiler will throw and error and abort compilation.