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

前端 未结 4 914
失恋的感觉
失恋的感觉 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 12:21

    val : immutable data variable

    var : mutable data variable

    When you converted your Java code to Kotlin:

    1. A converter found that you have not initialised variables, so it made them var(mutable) as you will initialise them later.

    2. Probably your variables are unused, so the converter made them internal, guessing you will not use them outside of your package.

    For more information on var and var read this, and for internal read this.

提交回复
热议问题