Understanding Types
Learn the basic data types used Kotlin and how these types are inferred in the language by declaring variables and functions.
We'll cover the following...
We said that Kotlin is a type-safe language. Let’s examine the Kotlin type system and compare it to what Java provides.
Note: The Java examples are for familiarity and not to prove that Kotlin is superior to Java in any way.
Basic types
Some languages make a distinction between primitive types and objects. Taking Java as an example, there is the int
type and Integer
—the former being more memory-efficient and the latter more expressive by supporting a lack of value and having methods.
There is no such distinction in Kotlin. From a developer’s perspective, all the types are the same.
But this doesn’t mean that Kotlin is less efficient than Java in that aspect. The Kotlin compiler optimizes types.
Most of the Kotlin types are named similarly to Java, the exceptions being Java’s Integer
being called Int
in ...