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

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.
主站蜘蛛池模板: 慈利县| 怀安县| 昆山市| 达孜县| 黄陵县| 嘉峪关市| 喀什市| 皮山县| 定日县| 塘沽区| 囊谦县| 安仁县| 攀枝花市| 桂东县| 榆社县| 三台县| 金坛市| 都江堰市| 开平市| 翼城县| 镇巴县| 井陉县| 耒阳市| 宝清县| 琼海市| 从江县| 讷河市| 河北省| 海安县| 余干县| 临高县| 会理县| 张家口市| 平远县| 宝兴县| 昭苏县| 巴彦淖尔市| 和静县| 拜城县| 美姑县| 武胜县|