Let’s look at the syntax of how to declare a variable in Scala.
svg viewer
Now, let’s map the syntax to actual code in Scala.
In the code above, we are declaring a variable with the name myFirstScalaVariable. myFirstScalaVariable can store data of type Int and is assigned an initial value of
5
5
. It is an immutable variable because we chose the keyword val.
As stated above, when declaring a variable, we need to define its type of data and set an initial value. However, in Scala, we also need to define the type of variable by mentioning a keyword.
But what are keywords and how do we choose the right one for our variable?