Variables in Kotlin, differences with Java: 'var' vs. 'val'?

前端 未结 4 875
失恋的感觉
失恋的感觉 2021-02-05 11:21

I am trying to learn Kotlin. What is val, var and internal in Kotlin compared to Java?

In Java:

 RadioGroup radio         


        
4条回答
  •  野的像风
    2021-02-05 11:54

    In kotlin we can declare variable in two types: val and var. val cannot be reassigned, it works as a final variable.

    val x = 2
    x=3 // cannot be reassigned
    

    On the other side, var can be reassigned it is mutable

    var x = 2
    x=3 // can be reassigned
    

提交回复
热议问题