当前位置:网站首页>Kotlin introductory notes (I) kotlin variables and non variables

Kotlin introductory notes (I) kotlin variables and non variables

2022-07-05 09:13:00 Meng Meng Mu Xi

Preface : This tutorial is best done with JAVA Study on the basis of

One 、 Variable

Kotlin Excellent type derivation mechanism

  •  val :(value Abbreviation ) Used to declare an immutable variable , This variable cannot be re assigned after the initial assignment , Corresponding Java Medium final Variable .
  •  var :(variable Abbreviation ) Used to declare a variable , This kind of variable can still be re assigned after the initial assignment , Corresponding Java Non - final Variable .

Such as :

fun main() {
    val num = 666
    println("num = " + num)
}

there num It is automatically deduced into Int ( notes :kotlin Abandoned int, All use object data types   Such as :Int)

however , If we delay the assignment of a variable ,kotlin You can't automatically deduce , We need to use the following method

Kotlin You can display declared variables

val num:Int = 10

We explicitly declare variables num by Int type , here Kotlin Don't try type derivation anymore .

Java and Kotlin Data type comparison table

Java Basic data type Kotlin data type Data type description

int

Int integer
longLong Long integer
shortShort Short
floatFloat Single-precision floating-point
doubleDouble Double precision floating point
booleanBoolean Boolean type
charChar Character
byteByte Character

Kotlin The small white , When learning the works of teacher Guolin , Take notes . If there are any mistakes, please point out ! Thank you for reading !

Reference resources :《 First line of code Android ( The third edition )》   --- Guo Lin

原网站

版权声明
本文为[Meng Meng Mu Xi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140538292673.html