I am Scala beginner and started going through "Programming in Scala". I came across the following code.
for (arg <- args)
println(arg)
In the book it say's arg is of type val and not var. Why is it so.
As per my understanding for each iteration the value of arg is changed, as it holds new value each time it is looped.
Following are my questions
- Why is arg val and not var ?
- As per Scala if it is val we cannot change the value (Since it is final in Scala)
- Also will it create multiple objects for each iteration and how are the objects destroyed after the loop ?
Also i know the difference between var and val in scala. I have gone across this link. What is the difference between a var and val definition in Scala?