官术网_书友最值得收藏!

Basic types in Kotlin

One of the big changes in Kotlin from Java is that, in Kotlin, everything is an object. If you come from a Java background, then you will already be aware that in Java there are special primitive types that are treated differently from objects. They cannot be used as generic types, do not support method/function calls, and cannot be assigned null. An example is the boolean primitive type.

Java introduced wrapper objects to offer a workaround in which primitive types are wrapped in objects, so that java.lang.Boolean wraps a boolean primitive type in order to smooth over the distinctions. Kotlin removes this necessity entirely from the language by promoting the primitives to full objects.

Whenever possible, the Kotlin compiler will map basic types back to JVM primitives for performance reasons. However, sometimes the values must be boxed, such as when the type is nullable, or when it is used in generics. Boxing is the conversion from a primitive type to a wrapper type, which takes place whenever an object is required but a primitive is presented.

Two different values that are boxed might not use the same instance, so referential equality is not guaranteed on boxed values.
主站蜘蛛池模板: 含山县| 格尔木市| 北川| 沽源县| 安岳县| 宜都市| 美姑县| 合肥市| 房山区| 琼中| 呼图壁县| 红安县| 宁国市| 霍州市| 青河县| 九寨沟县| 云霄县| 蛟河市| 马尔康县| 南投县| 平谷区| 阜南县| 绥阳县| 克拉玛依市| 上杭县| 大方县| 定兴县| 梧州市| 福泉市| 长宁县| 渭源县| 石景山区| 海盐县| 宜阳县| 汉中市| 永年县| 介休市| 阿鲁科尔沁旗| 沙雅县| 扶绥县| 夏津县|