Difference Between Val, Lazy Val and Def

  1. val

    1
    2
    3
    4
    5
    6
    scala> val a = { println("hello, world."); 3 }
    hello, world.
    a: Int = 3

    scala> a
    res0: Int = 3
  2. lazy val

    1
    2
    3
    4
    5
    6
    7
    8
    9
    scala> lazy val a = { println("hello, world."); 3 }
    a: Int = <lazy>

    scala> a
    hello, world.
    res1: Int = 3

    scala> a
    res2: Int = 3
  3. def

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    scala> def a = { println("hello, world."); 3 }
    a: Int

    scala> a
    hello, world.
    res4: Int = 3

    scala> a
    hello, world.
    res5: Int = 3

TBD