Scala中的var和val定义有什么区别?为什么这两种定义都需要?为什么你会选择val而不是var,反之亦然?


当前回答

在javascript方面,它与

val -> const 是 -> 是

其他回答

val表示不可变,var表示可变

解释一下,“val表示值,var表示变量”。

A distinction that happens to be extremely important in computing (because those two concepts define the very essence of what programming is all about), and that OO has managed to blur almost completely, because in OO, the only axiom is that "everything is an object". And that as a consequence, lots of programmers these days tend not to understand/appreciate/recognize, because they have been brainwashed into "thinking the OO way" exclusively. Often leading to variable/mutable objects being used like everywhere, when value/immutable objects might/would often have been better.

在javascript方面,它与

val -> const 是 -> 是

简单来说:

var = 变量

Val =变量+ final

Val表示最终值,不能重新赋值

而Var可以在以后重新分配。

就像它的名字一样简单。

Var表示它可以变化 Val表示不变