scala> // #PS:89
scala> // psn > a30 > a70
scala> // Use Lazy Val when we want to defer
scala> // initializing some LARGE data only when it is needed
scala> // (ie the first time it has been used)
scala> // Do not use Lazy val for small value initialization
scala> // as creating lazy val has overhead associated with it
scala> def getNo():Int = {
| println("Inside method getNo()")
| 5
| }
getNo: ()Int
scala> // Note here... getNo() is NOT called at this time
scala> lazy val no = getNo()
no: Int =
scala> //getNo() is called NOW (Lazily evaluaeted)
scala> val result = no + 6
Inside method getNo()
result: Int = 11
scala> // getNo() is not called here(ie getNo() is called only once)
scala> val result = no + 11
result: Int = 16
Lazy Val : Example
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment