Tutorials

Scala Tutorial: var vs val

When it comes to variable assignment, newcomers to Scala are often confused by the difference between val and var. In this tutorial, we explain when to use val vs var and provide some basic examples of using each.

Key Takeaways

  • val is used for immutable variables that cannot be reassigned.
  • var is used for mutable variables that can be reassigned.
  • Both val and var can hold references to mutable objects.
  • Immutable programming practices by using val are generally preferred in Scala.

When to use val

When you assign a variable with val, you are saving the output of some computed value for later use. This makes val immutable, meaning it cannot be changed after initial assignment.

Example:

val name = "Joe"
name = "Ted" //error: reassignment to val

In the example above, notice how we declare an immutable variable name using val. If we try to reassign the variable, we get an error.

As a general rule, use val when you want to assign an immutable value that won't change. It's the default choice whenever immutability is acceptable, promoting safer, more predictable code.

When to use var

When you assign a variable with var, you are creating a mutable variable. This means you can reassign the value after its initial assignment.

Example:

var name = "Joe"
name = "Ted" // reassigns the variable name to "Ted"

In the above example, we are able to reassign the variable name because we declared it using var.

Note: Though var offers flexibility, excessive use can lead to less reliable code, so prefer val unless mutation is necessary.

Immutability vs. Mutable References

Although using val means immutability in terms of reassignment, it doesn't mean the mutable value itself can't be altered. Here's a classic example with arrays:

val array = Array(1, 2, 3)
array(0) = 4 // array becomes Array(4, 2, 3)
array = Array(5, 6, 7) //error: reassignment to val

In this example, you can modify the existing array variable because you are changing its contents, not its reference. Attempting to reassign array to a new instance results in a reassignment error.

Conclusion

Use var when you need a mutable variable that you can reassign. Use val when you want to reference the output of an operation for later use without changing the reference itself. Remember that while val represents immutability, it doesn't mean mutable values assigned to val can't change.

FAQ

Why should I prefer val over var?

Preference for val over var is due to its immutability, which leads to safer and more predictable code by reducing side effects.

Can I change the contents of an object assigned to a val?

Yes, while you can't reassign a val, you can modify the contents of a mutable object it references, such as a list or array.

Is using var always a bad idea in Scala?

Not necessarily. While some discourage using var, it's valid when you need mutable state and are careful about concurrency and side effects.

What happens if I try to reassign a val?

You’ll see a compilation error, as val prohibits reassignment once initialized.

Mastering the tech interviewWhat everyone is doing wrong in tech interviews