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

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.

主站蜘蛛池模板: 江北区| 张家港市| 河源市| 米林县| 黎平县| 鱼台县| 峨眉山市| 梧州市| 闽清县| 怀安县| 那曲县| 禹州市| 清苑县| 砚山县| 泗水县| 拉孜县| 汶川县| 南乐县| 平陆县| 蒙山县| 介休市| 高尔夫| 龙胜| 四会市| 丰都县| 朝阳区| 龙游县| 嘉禾县| 融水| 望都县| 淮南市| 洛阳市| 盐亭县| 读书| 澎湖县| 准格尔旗| 洪泽县| 旌德县| 灵川县| 怀化市| 雷波县|