July 2017
Intermediate to advanced
796 pages
18h 55m
English
According to the section earlier, val is used to declare immutable variables, so can we change the values of these variables? Will it be similar to the final keyword in Java? To help us understand more about this, we will use the following code snippet:
scala> var testVar = 10testVar: Int = 10scala> testVar = testVar + 10testVar: Int = 20scala> val testVal = 6testVal: Int = 6scala> testVal = testVal + 10<console>:12: error: reassignment to val testVal = testVal + 10 ^scala>
If you run the preceding code, an error at compilation time will be noticed, which will tell you that you are trying to reassign to a val variable. In general, mutable variables bring a performance advantage. The reason is that this ...
Read now
Unlock full access