1.

What is the difference between val and lazy val?

Answer»

In Scala, val and LAZY val both are used to declare a value. When we declare a val and ASSIGN some expression in it, the expression is evaluated and assigned in that place. Whereas, for lazy val, the expression is evaluated and the value is assigned at the time of its first use.

Lazy VALS are useful when OBJECT creation or expression evaluation is costly in terms of CPU and memory and the value may or may not be used based on some condition.



Discussion

No Comment Found