In Previous chapter we learned about SCALA MUTABILITY VS IMMUTABILITY and today lets check out SCALA LAZY EVALUATION.


In Scala Lazy Evaluation the evaluation of an expression is delayed until its first use. If you check Apache Spark the Transformations are Lazy in nature . But in Scala you have to specifically mention the Keyword Lazy to make the variable behave in a lazy way.

scala> val x =5
x: Int = 5

scala> lazy val y = 5
y: Int = <lazy>

scala> y
res0: Int = 5

In the above example we first initialized value 5 to x and it got immediately initialized . Next I initialized 5 to y using Keyword Lazy. Here you see that the value is not initialed as it returned y: Int = <lazy> . But as soon as you call the variable y you see that the variable is assigned, this is how LAZY works.

Wikipedia Page has some information on Lazy Evaluation which you can access here.

In the next chapter we will learn about SCALA STRING INTERPOLATION

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.