Google News
logo
Scala - Interview Questions
What is the difference between 'val' and 'def' in Scala?
In Scala, `val` and `def` are used to define values and methods, respectively. While both `val` and `def` are used to define named entities, they have distinct characteristics and purposes.

Here are the main differences between `val` and `def`:

1. Evaluation :

   * `val`: A `val` defines a value that is computed once and assigned at the time of declaration. It is evaluated eagerly and the result is stored. The computed value remains constant throughout its lifetime. Think of `val` as a variable that cannot be reassigned.
   
   * `def`: A `def` defines a method, which is a reusable block of code that can be invoked with arguments. When a method is called, its body is evaluated and the result is returned. Methods are evaluated lazily, meaning the computation happens when the method is invoked.

2. Assignment and Reassignment :

   * `val`: A `val` is assigned a value once at the time of declaration and cannot be reassigned. It represents an immutable value. If you try to reassign a `val`, it will result in a compilation error.
   
   * `def`: A `def` is not directly assignable or reassignable. It represents a callable block of code that can be invoked with different arguments. You can think of `def` as a function or method declaration.

3. Memory Allocation :

   * `val`: A `val` allocates memory space for storing the computed value at the time of declaration. The allocated memory is released when the variable goes out of scope.
   
   * `def`: A `def` does not allocate memory space for the method itself. Instead, it defines a code block that is evaluated when the method is called.

4. Eager vs. Lazy Evaluation :

   * `val`: A `val` is evaluated eagerly at the time of declaration. The value is computed and assigned immediately.
   
   * `def`: A `def` is evaluated lazily, meaning the computation is deferred until the method is called. The code inside the method body is executed when the method is invoked.
5. Use Cases :

   * `val`: Use `val` when you want to define an immutable value that is computed once and remains constant.
   
   * `def`: Use `def` when you want to define a reusable block of code that can be invoked with different arguments. Methods allow for code abstraction and reuse.

Here's an example that demonstrates the differences between `val` and `def`:
val x: Int = {
  println("Computing x")
  42
}

def y: Int = {
  println("Computing y")
  42
}

println(x) // Output: Computing x, 42
println(x) // Output: 42 (value is not recomputed)

println(y) // Output: Computing y, 42
println(y) // Output: Computing y, 42 (method body is evaluated each time)​

In this example, `x` is a `val` that is computed eagerly at the time of declaration. The value is assigned once and remains constant throughout its lifetime. The println statement inside the `val` block is executed only once.

On the other hand, `y` is a `def` that represents a method. The method body is evaluated each time the method is called. The println statement inside the method body is executed each time the method is invoked.
Advertisement