Scala Variables

Variable in Scala is defined as memory locations reserved to store values. When a variable is created, some memory is reserved for the space it will use.

Scala has mainly two types of variables, mutable and Immutable.

Mutable Variable (var)

Mutable variables are defined using var keywords. Its value can be changed after assigning it.

scala> var x=30
x: Int = 30

scala> x = 20
x: Int = 20 // Value of Var can be changed 

Immutable variable (val)

Immutable variables are defined using val keywords. Its value cannot be changed after assigning it.

scala> val y = 20
y: Int = 20

//Compiler will throw an error as value of val cannot be reassigned
scala> y=10 
<console>:12: error: reassignment to val
       y=10

Type Inference in Scala Variable

Scala is a statically typed language. Scala compiler infers the type of variable whenever possible without forcing the developers to declare it.

Below, two statements mean the same thing.

val y =20
val y1: Int = 20

// Below two means the same thing
val x = 'c'
val x1:Char= 'c'