Kotlin 术语 "mutable" 错了吗?
Is the Kotlin term "mutable" wrong?
我的理解是,“变量”一词指的是引用被重新分配的能力。 “常量”意味着不能重新分配引用。 Java.
中final与not的本质区别
var something = new obj() -> reference can be re-assigned
val something = new obj() -> cannot be re-assigned
对我来说,“可变性”意味着能够修改 REFERAND/OBJECT 本身,而不是它的引用。 IE。 被引用的对象。但是 Kotlin 并没有阻止这种情况。
你可以
val something = new obj()
但仍然能够在不重新分配给新标识符的情况下“改变”obj()。
我是不是误会了什么,还是用词不当?
val
and var
只控制引用的不可变性而不是它指向的对象。
There are two keywords to declare a variable:
val
(from value)—Immutable reference. A variable declared with val
can’t be reassigned after it’s initialized. It corresponds to a final
variable in Java.
var
(from variable)—Mutable reference. The value of such a variable can be changed. This declaration corresponds to a regular
(non-final) Java variable.
Note that, even though a val
reference is itself immutable and can’t
be changed, the object that it points to may be mutable. For example,
this code is perfectly valid:
val languages = arrayListOf("Java")
languages.add("Kotlin")
我的理解是,“变量”一词指的是引用被重新分配的能力。 “常量”意味着不能重新分配引用。 Java.
中final与not的本质区别var something = new obj() -> reference can be re-assigned
val something = new obj() -> cannot be re-assigned
对我来说,“可变性”意味着能够修改 REFERAND/OBJECT 本身,而不是它的引用。 IE。 被引用的对象。但是 Kotlin 并没有阻止这种情况。
你可以
val something = new obj()
但仍然能够在不重新分配给新标识符的情况下“改变”obj()。
我是不是误会了什么,还是用词不当?
val
and var
只控制引用的不可变性而不是它指向的对象。
There are two keywords to declare a variable:
val
(from value)—Immutable reference. A variable declared withval
can’t be reassigned after it’s initialized. It corresponds to afinal
variable in Java.var
(from variable)—Mutable reference. The value of such a variable can be changed. This declaration corresponds to a regular (non-final) Java variable.Note that, even though a
val
reference is itself immutable and can’t be changed, the object that it points to may be mutable. For example, this code is perfectly valid:val languages = arrayListOf("Java") languages.add("Kotlin")