Difference between var, val, and def in Scala

This is one of the frequently asked questions from Scala interviews. Even though both var and val keyword is used to declare variables in Scala there is a subtle difference between them. A var is a variable. It’s a mutable reference to a value. Since it’s mutable, its value may change through the program lifetime. On the other hand, val keyword represents a value. It’s an immutable reference, meaning that its value never changes. Once assigned it will always keep the same value. It’s similar to a final variable in Java or constants in other languages. It's also worth to remember that the variable type cannot change in Scala. You may say that a var behaves similarly to Java variables.

While the def is a function declaration. It is evaluated on call, similar to Python, where def is also used to declare a function. Let's see some code example of var and val in Scala to understand the difference in more detail.

Difference between var, val, and def in Scala

Here are some code snippets to show you how exactly var, val and def are used in Scala programming language to declare variables, values, and functions.

Here is code example of val works in Scala:

var x = 10

After this declare var x becomes a variable of type Int. You can re-assign it a value e.g.

x = 14

Above should be fine and accepted by Scala compiler because 14 is also of type Int but below will not work because "four" is not Int and you cannot change the type of var in Scala.

x = "four" // not accepted by the scala compiler 

Now, let's see some code example of how to use val keyword in Scala

val y = 13

After this assignment y becomes an Int variable but it's constant which means you cannot assign another value to variable y, that's why it's known as value instead of a variable. You can read more about Scala type system in "Programming in Scala" book by Martin Oderskey, the author of Scala programming language.

Below, reassignment will produce an error 'error: reassignment to val'

y = 14

Now, let's see how you can use the def keyword to declare functions in Scala:

def hello(name: String) = "Hello : " + name

This is a function which takes a String and prints String

hello("OOP") // "Hello : OOP"
hello("FP") //  "Hello : FP"

Scala also supports lazy evaluation of val, also known as lazy val. It’s similar to a val, but its value is only computed when needed and it is defined using the lazy keyword. It’s especially useful to avoid heavy computations e.g. by using short-circuit && and || logical variables.

Suppose you have two val as shown below, one is lazy val and other is a normal one.

lazy val x = {
  println("calculating value of x")

val y = {
  println("calculating value of y")

Now, if you perform the following operation

y + y 

Then the x was still not be evaluated i.e. "calculating the values of x" will not be printed because you don't need the value of x yet.

x + x

Now, you need the value of x hence the value of x will be calculated and you will see the message "calculating the value of x" but only once.

Here is the nice summary of the difference between val and var Scala variables:

Difference between var, val, and def in Scala

That's all about difference between var, val, and def in Scala. In short, the val and var are evaluated when defined, while def is evaluated on call. Also, val defines a constant, a fixed value which cannot be modified once declared and assigned while var defines a variable, which can be modified or reassigned.  If you are learning Scala, then I also suggest reading a good book on Scala e.g. "Scala for the Impatient" by Cay S. Horstmann to fill the gaps and get a good understanding of fundamentals.

No comments:

Post a Comment