Difference between var, val, and def in Scala? Examples

Hello guys, if you are new to Scala or preparing for Scala interview then you may have come across the question, what is difference between var, val, and def keywords 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 throughout the program's lifetime. On the other hand, the 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.

Scala's val is similar to a final variable in Java or constants in other languages. It's also worth remembering 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 examples 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 a 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 like:

x = 14

Above should be fine and accepted by the 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 examples of how to use the 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 a value instead of a variable. You can read more about Scala type system in "Programming in Scala" book by Martin Odersky, 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 that 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 like by using short-circuit && and || logical variables. You can see these free Scala courses to learn more about them. 

Suppose you have two val as shown below, one is lazy val, and the 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 a nice summary of the difference between val and var Scala variables:

Difference between var, val, and def in Scala

That's all about the 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 that 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 joining one of these Scala courses for beginners to fill the gaps and get a good understanding of fundamentals.

Other Scala articles and Tutorials you may like to read

Thanks you for reading this article till the end. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.