Variables in Scala
Before entering into the depth of OOP features, first, we need to details about the different types of variables and data types in Scala. To declare a variable in Scala, you need to use var
or val
keywords. The formal syntax of declaring a variable in Scala is as follows:
val or var VariableName : DataType = Initial_Value
For example, let's see how can we declare two variables whose data types are explicitly specified as follows:
var myVar : Int = 50 val myVal : String = "Hello World! I've started learning Scala."
You can even just declare a variable without specifying the DataType
. For example, let's see how to declare a variable using val
or var
, as follows:
var myVar = 50 val myVal = "Hello World! I've started learning Scala."
There are two types of in Scala: mutable and immutable that can be defined as follows:
- Mutable: The ones whose values you can change later
- Immutable: The ones values you cannot change once they have been set
In general, for declaring a mutable variable...