Scala Learning Series
Scala: Defining Variables Using the “def” Keyword
Let’s know the difference between defining variables using def
and val
or val
.
The title reads as follows: “Defining Variables Using the def Keyword.” Before we continue, let’s see if this title is appropriate. It’s not!
Let’s first be clear that a def
is not a variable declaration. It's a declaration of a function or method instead.
Consider the following code:
scala> def i = 3
def i: Intscala> i.getClass
val res0: Class[Int] = intscala> println(i)
3scala> val j = 3
val j: Int = 3scala> j.getClass
val res1: Class[Int] = intscala> println(j)
3scala> i + j
val res2: Int = 6
As we see above, variable definition works using def
. Unlike a val
or var
declaration, a def
is not a variable declaration. It's a function declaration. What we are doing is creating a function or method that returns a constant number 3. Having said that, both i
and j
are functions that do not take any arguments.
Unless the lazy
keyword is prefixed, variables declared with the val
or var
are evaluated immediately, whereas def
is evaluated on call, which means it is lazy evaluated. Having said, def
evaluation happens when called explicitly.
Note that Scala does not permit the creation of
lazy
variables, i.e.,var
. Onlylazy
values (val
) are allowed.
Difference between “lazy val” and “def”
Difference between lazy val
and def
:
- When accessed for the first time,
lazy
evaluates and caches the result. - The
def
declaration is evaluated every time we call method name.
Let’s see the difference between lazy val
and def
in action:
scala> lazy val a = { println("a value evaluated"); 1}
lazy val a: Intscala> a
a value evaluated
val res0: Int = 1scala> a
val res1: Int = 1scala> def b = { println("b function evaluated"); 2}
def b: Intscala> a
b function evaluated
val res3: Int = 2scala> a
b function evaluated
val res4: Int = 2
As we see above, a
is evaluated only once, and any subsequent invocations of a
return the cached result. This indicates that lazy val
only needs to be evaluated once and then the result is stored forever.
On the other hand, a def
is evaluated every time it is invoked. In this case, we see println
output every time we invoke the function.