When creating a variable using val or var and assigning the value of an expression into it the expression is evaluated when the variable is created. When creating a function using def and assigning the value of an expression to it the expression is evaluated when calling the function.

object Program {
  def main(args: Array[String]):Unit =
  {
    var a = 3
    var b = 4
    var c = 5
    val e1 = if (b>a) "spiderman" else "wonderwoman"
    var e2 = if (b>a) "spiderman" else "wonderwoman"
    def e3 = if (b>a) "spiderman" else "wonderwoman"
    a = 7
    println(e1)
    println(e2)
    println(e3)
  }
}

The following video clip goes over this code sample and explains it.

Leave a Reply

Your email address will not be published. Required fields are marked *