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

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.

主站蜘蛛池模板: 崇仁县| 安化县| 南溪县| 太湖县| 十堰市| 望江县| 商洛市| 康定县| 旌德县| 阿鲁科尔沁旗| 平和县| 永嘉县| 西昌市| 北安市| 西林县| 牡丹江市| 从江县| 宣城市| 蒙城县| 临夏市| 奈曼旗| 惠来县| 萨嘎县| 涟水县| 蒙阴县| 墨竹工卡县| 林口县| 资溪县| 桐柏县| 无棣县| 潼关县| 黄梅县| 永仁县| 卓尼县| 会宁县| 吴旗县| 亚东县| 茶陵县| 米脂县| 钟祥市| 固始县|