Scala variables – var vs val

Reading Time: 3 minutes

We know in any programming language Variables are used to store information to be referenced and manipulated. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. Scala – a multi-paradigm programming language allows one to declare variables mutable or immutable. We can create mutable variables via keyword var and immutable variables via val keyword. Let’s understand each one of these variables in detail.

Mutable Variable:

These are those variables which allow us to change a value throughout its lifetime after the declaration of a variable.

Syntax:

var Variable_name: Data_type = "value";

Scala has a built-in type inference mechanism which allows the programmer to omit certain type annotations. So, we can omit Data_type from the above. For more about type inference type inference .

Example

class Person {
  var name: String = "Admin"
  var age : Int = 20
}

In the above code snippet, created a Person class with two fields – name and age prefixed with keyword var which makes them mutable. Let’s disassemble the above Scala code. Run the following command:

:javap -p Person

Mutable fields get compiled into:
private field
public getter method

public setter method as above
Let’s create an object for the Person class and modifies the name.

We can easily mutate the variable which are declare as var.

Immutable Variable

These variables are those variables which do not allow us to change or reassign a value once initialized.

Syntax:

val Variable_name: Data_type =  "value";

Example

class Person {
  val name: String = "Admin"
  val age : Int = 20
}

In the above code snippet created a class Person with two fields – name and age prefixed with keyword val which makes them Immutable.Let’s disassemble the above Scala code. Run the following command:

:javap -p Person

An immutable fields get compiled into:
private final field
public getter method
as above
We can see both the members has modifier final and no setter method so, we can’t mutate the fields.

We get an error reassignment to val.

Which one use: Mutable or Immutable?

Some programming languages only allow for immutable variables (e.g. Haskell) and other languages only allow for mutable variables (e.g. Ruby). Scala let’s programmers choose if they want to use immutable variables or mutable variables.

Immutable variables make the code free of side effects and it result of a pure function which can be tested easily. Also immutable objects won’t lead to concurrency issues that’s why it is a best practice to use immutable objects.

Use var only when have a specific use cases. For example, in GUI programming, mutable object are very handy. Think about a game character. In games, speed is top priority, so representing our game characters with mutable objects will most likely make your game run significantly faster than an alternative implementation where a new copy of the game character is spawned for every little change

Conclusion

We have seen mutable objects can be create by using var keyword on the other hand, immutable objects can be create by using val keyword. The Scala design philosophy gives us a decision making power to choose either mutable or immutable objects. As Immutability makes concurrent programming way safer and cleaner, it prefers most as best practice. But we can also use mutable objects for a specific use case.