zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Scala val 和 var 的区别

scala 区别 var val
2023-09-11 14:14:33 时间

Scala官网:https://docs.scala-lang.org/tour/basics.html

val(Values)

  Named results, such as x here, are called values. Referencing a value does not re-compute it.

  x = 3 // This does not compile.

var(Variables)

  Variables are like values, except you can re-assign them. You can define a variable with the var keyword.

  var x = 1 + 1

  x = 3 // This compiles because "x" is declared with the "var" keyword.

  println(x * x) // 9