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

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.
主站蜘蛛池模板: 东兰县| 东安县| 湟中县| 闽清县| 仙居县| 荥阳市| 江西省| 信阳市| 凤山县| 东安县| 安宁市| 临高县| 钟祥市| 桐城市| 大洼县| 漠河县| 鸡泽县| 汉源县| 芦溪县| 江陵县| 平武县| 富源县| 涟源市| 新沂市| 隆子县| 信阳市| 宣武区| 高陵县| 新民市| 勐海县| 丰镇市| 神木县| 苍梧县| 繁峙县| 沂南县| 邢台县| 崇义县| 岢岚县| 吉安县| 蓬安县| 双辽市|