Scala by Example

Scala is a multi-paradigm, static, and strongly typed language on the JVM.

Source

Variables

Variables are defined using the ‘var’ keyword:

var name: String = "Sherlock"

Result:

name: String = Sherlock

Notice that the type of variables is declared after the variable name separated by a colon mark.

Another way to declare variables:

var name = "John"

Result:

name: String = John

Scala can infer the types in most cases so it is not necessary to declare variable types explicity.

Moreover, as you saw the data of variables can change. In the second statement we changed the data of variable name to “John”. Variables in Scala are mutable.