What is the difference between Int and Integer in Kotlin?

后端 未结 3 506
遇见更好的自我
遇见更好的自我 2021-02-05 06:33

I have tried using Int and Integer types in Kotlin.Although I don\'t see any difference. Is there a difference between Int and Integer types in Kotlin?Or are they the same?

3条回答
  •  灰色年华
    2021-02-05 07:33

    Just have a look at https://kotlinlang.org/docs/reference/basic-types.html#representation

    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.

    That means, that Int is represented as primitive int. Only in cases when nullability or generics are involved, the backing Wrapper Type Integer must be used. If you use Integer from the beginning, you always work with the wrapper type and never with the primitive int.

提交回复
热议问题