Does Kotlin have primitive types?

后端 未结 3 521
醉话见心
醉话见心 2021-02-08 10:30

Does Kotlin have primitive types?. When I declare the variable: val myAge: Int = 18 then the myAge variable stores the actual values is 18

3条回答
  •  无人共我
    2021-02-08 11:11

    On the Java platform, numbers are physically stored as JVM primitive types, unless we need a nullable number reference (e.g. Int?) or generics are involved. In the latter cases numbers are boxed.

    Note that boxing of numbers does not necessarily preserve identity:

    val a: Int = 10000
    println(a === a) // Prints 'true'
    val boxedA: Int? = a
    val anotherBoxedA: Int? = a
    println(boxedA === anotherBoxedA) // !!!Prints 'false'!!!
    

    Note "===" used to compare reference ....

    On the other hand, it preserves equality:

    val a: Int = 10000
    println(a == a) // Prints 'true'
    val boxedA: Int? = a
    val anotherBoxedA: Int? = a
    println(boxedA == anotherBoxedA) // Prints 'true'
    

提交回复
热议问题