This is a short Scala post to explain what the difference is between val and var. I was showing some Scala code to a co-worker this past week and he was asking what the difference was between val and var. It is quite simple:
val defines a fixed value, it is a read only variable
var defines a mutable variable, this variable can be modified
In Java you would use final to create a variable which would be read only, this is the same as val in Scala.
Let's look at some very simple Scala code.
Running the code above will give the following output
Test1 5
Test1 6
done with main
If you are trying to use val, you will get an error, change var to val and see if you can compile the code
Here is the error, the code won't even compile
Description Resource Path Location Type reassignment to val Test.scala /ScalaTemp/src line 8 Scala Problem
So as you can see, val is read only, while with var you can modify the variable.
If you want to play around with Scala, take a look at Installing Scala 2.10 on Eclipse Juno






LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.