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

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.

主站蜘蛛池模板: 洪雅县| 株洲县| 贺兰县| 太白县| 广德县| 山东| 赤水市| 巨鹿县| 迭部县| 滨海县| 六盘水市| 诸暨市| 巧家县| 叶城县| 隆昌县| 海伦市| 静海县| 云南省| 沁水县| 滨州市| 吉木乃县| 东至县| 米脂县| 玉屏| 台南县| 巨野县| 卢龙县| 金昌市| 渝北区| 旌德县| 庆城县| 金阳县| 信丰县| 林甸县| 罗田县| 依兰县| 额济纳旗| 玛多县| 名山县| 抚松县| 美姑县|