Does Kotlin have primitive types?

后端 未结 3 520
醉话见心
醉话见心 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 10:48

    Short answer - yes and depends on the declaration.

    val myAge: Int = 18           // this is primitive
    val myAge2: Int? = 18         // this is not
    

    There's a very informative video about that https://www.youtube.com/watch?v=Ta5wBJsC39s

    0 讨论(0)
  • 2021-02-08 11:07

    No... and yes.

    Kotlin doesn't have primitive type (I mean you cannot declare primitive directly). It uses classes like Int, Float as an object wrapper for primitives.

    When kotlin code is converted to jvm code, whenever possible, "primitive object" is converted to java primitive. In some cases this cannot be done. Those cases are, for example, collection of "primitives". For example, List<Int> cannot contains primitive. So, compiler knows when it can convert object to primitive. And, again, it's very similar to java:

    List<Integer> numbers = new ArrayList<>;
    
    numbers.add(0); // <-- you use primitive, but in fact, JVM will convert this primitive to object.
    numbers.add(new Integer(0)); // <-- We don't need do that.
    

    Also, when you declare "nullable primitive" it is never converted to primitive (what is kind of obvious, as primitive cannot be null). In java it works very similar:

    int k = null; // No way!
    Integer kN = null; // That's OK.
    

    One more thing - what docs are saying about it?

    For Common, JVM, JS

    Represents a 32-bit signed integer. On the JVM, non-nullable values of this type are represented as values of the primitive type int.

    For Native

    Represents a 32-bit signed integer.

    @see: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html

    So, the last conclusion. Kotlin doesn't have primitive types out of the box. You treat all objects like objects. Converting to primitive is done at some lower level than code. This design is caused to keep compatibility with JVM.

    I did a little deep dive and published it on medium. For interested: https://medium.com/@przemek.materna/kotlin-is-not-primitive-primitives-in-kotlin-and-java-f35713fda5cd

    0 讨论(0)
  • 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'
    
    0 讨论(0)
提交回复
热议问题