What are Scala – Variables?

If you are interested to learn about the Scala Data Type

Variables are nothing but reserved memory locations to store values. This means that when you create a variable, you reserve some space in memory.

Based on the data type of a variable, the compiler allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.

Variable Declaration

Scala has a different syntax for declaring variables. They can be defined as value, i.e., constant or a variable. Here, myVar is declared using the keyword var. It is a variable that can change value and this is called mutable variable. Following is the syntax to define a variable using var keyword −


var myVar : String = "Foo"

Here, myVal is declared using the keyword val. This means that it is a variable that cannot be changed and this is called immutable variable. Following is the syntax to define a variable using val keyword −


val myVal : String = "Foo"

Variable Data Types

The type of a variable is specified after the variable name and before equals sign.There are 9 data types in Scala: ByteShortIntLongFloatDoubleCharBoolean and Unit. If you have a previous experience with Java, you might know that Java has 8 primitive data types – all of them are enumerated above (exclude Unit), except . Furthermore, in Scala any data type is an object, everything is object, even functions are objects. This distinguishes Scala from Java, it doesn’t has primitives.

Since all of data types are objects, you can use a wide range of useful methods defined for them. For example transformation from one type to another (Int to Double and vice versa) etc.

You can define any type of Scala variable by mentioning its data type as follows −


val or val VariableName : DataType = [Initial Value]

If you do not assign any initial value to a variable, then it is valid as follows −


var myVar :Int;
val myVal :String;

Variable Type Inference

When you assign an initial value to a variable, the Scala compiler can figure out the type of the variable based on the value assigned to it. This is called variable type inference. Therefore, you could write these variable declarations like this −


var myVar = 10;
val myVal = "Hello, Scala!";

Here, by default, myVar will be Int type and myVal will become String type variable.

Multiple assignments

Scala supports multiple assignments. If a code block or method returns a Tuple (Tuple − Holds collection of Objects of different types), the Tuple can be assigned to a val variable. [Note − We will study Tuples in subsequent chapters.]


val (myVar1: Int, myVar2: String) = Pair(40, "Foo")

And the type inference gets it right −


val (myVar1, myVar2) = Pair(40, "Foo")

Example Program

The following is an example program that explains the process of variable declaration in Scala. This program declares four variables — two variables are defined with type declaration and remaining two are without type declaration.


object Demo {
   def main(args: Array[String]) {
      var myVar :Int = 10;
      val myVal :String = "Hello Scala with datatype declaration.";
      var myVar1 = 20;
      val myVal1 = "Hello Scala new without datatype declaration.";
      println(myVar); println(myVal); println(myVar1); 

Save the above program in Demo.scala. The following commands are used to compile and execute this program.


\>scalac Demo.scala
\>scala Demo


Hello Scala with datatype declaration.
Hello Scala without datatype declaration.

Variable Scope

Variables in Scala can have three different scopes depending on the place where they are being used. They can exist as fields, as method parameters and as local variables. Below are the details about each type of scope.


Fields are variables that belong to an object. The fields are accessible from inside every method in the object. Fields can also be accessible outside the object depending on what access modifiers the field is declared with. Object fields can be both mutable and immutable types and can be defined using either var or val.

Method Parameters

Method parameters are variables, which are used to pass the value inside a method, when the method is called. Method parameters are only accessible from inside the method but the objects passed in may be accessible from the outside, if you have a reference to the object from outside the method. Method parameters are always immutable which are defined by val keyword.

Local Variables

Local variables are variables declared inside a method. Local variables are only accessible from inside the method, but the objects you create may escape the method if you return them from the method. Local variables can be both mutable and immutable types and can be defined using either var or val.

Rules Defining Variable in Scala

The rules and convention for defining the variables in Scala are as follows:

  1. The variable name starts with a lower camel case. For example, in the variable name ‘nameOfGirl,’ contains ‘n’ as the small alphabet. Afterward, capitalize all the beginning alphabet of the word like ‘O’ and ‘G.’
  2. The variable name should not contain the reserved word or keyword defined in Scala.
  3. The variable name can contain dollar sign (‘$’) and the underscore (‘_’) sign. However, other special characters such as ‘#’,’&’, etc., are not allowed. Also, using underscore is discouraged.
  4. White space (tabs, space) is not allowed in a variable name.
  5. The variable name must start with a letter and cannot begin with a number or other characters.
What are Scala – Variables?
Show Buttons
Hide Buttons