A Type describes the data that is held by a variable or a constant and tells the compiler how type checking will be handled.
| Type | Description | Type | Description |
|---|---|---|---|
String | “Text” | Char | Character |
Boolean | true | false | Int | Byte | Short | Int | Long |
Float | Double | Decimal Number | List | Collection of elements |
Set | Collection of unique elements | Map | Collection of key-value pairs |
Java types
In Java, there are two kinds of types: reference types and primitive types.
The name of reference types in Java begins with a Capital letter to indicate that they are backed by a source definition. E.g. Integer.
The names of Java primitive types start with a lowercase letter. E.g. int.
All primitives in Java have a corresponding reference type, but not all reference types have a corresponding primitive type.
One reason for choosing a reference type is that there are certain features of the Java language that are only available when using reference types. Generics, for example, don’t work with primitives. Reference types can also work with the object-oriented features of Java than its primitive values.
On the other hand, primitives offer better performance and some other perks.
Unlike Java, Kotlin provides only one kind of type: reference types.
Kotlin made this design decision for several reasons:
- If there is no choice between kinds of types, you cannot code yourself into a corner as easily as you can with multiple kinds to choose from. The Kotlin compiler will, where possible, use primitives in the Java bytecode.
