官术网_书友最值得收藏!

val and var

Kotlin has two keywords for declaring variables, val and var. The var variable is a mutable variable, that is, a variable that can be changed to another value by reassigning it. This is equivalent to declaring a variable in Java:

    var name = "kotlin" 

In addition to this, the var variable can be initialized later:

    var name: String 
    name = "kotlin" 

Variables defined with var can be reassigned, since they are mutable:

    var name = "kotlin" 
    name = "more kotlin" 

The val keyword is used to declare a read-only variable. This is equivalent to declaring a final variable in Java. A val variable must be initialized when it is created, since it cannot be changed later:

    val name = "kotlin" 

A read-only variable does not mean the instance itself is automatically immutable. The instance may still allow its member variables to be changed through functions or properties, but the variable itself cannot change its value or be reassigned to another value.

主站蜘蛛池模板: 新竹县| 米易县| 石首市| 万年县| 丽水市| 红桥区| 黄陵县| 南乐县| 蓬莱市| 大同市| 巴塘县| 正蓝旗| 东源县| 武陟县| 汝阳县| 阳原县| 自贡市| 庄浪县| 兴和县| 临朐县| 新化县| 苍溪县| 桃园市| 青海省| 长海县| 荔波县| 永宁县| 历史| 顺平县| 康保县| 叶城县| 伊宁县| 玉林市| 和林格尔县| 循化| 寿光市| 新民市| 桓仁| 城市| 新宾| 若羌县|