Basic types
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 which 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 primitive type boolean
.
Java introduced wrapper objects to offer a work around in which primitive types are wrapped in objects, so that java.lang.Boolean
wraps a boolean
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 that types place whenever...