Basic Data Types
Explore the fundamental data types in Kotlin including integers, floats, characters, and booleans. Understand Kotlin's static typing and how its types correspond to Java bytecode. This lesson helps you safely manage data values and prepares you for Kotlin's type inference and null safety concepts.
We'll cover the following...
Kotlin is a statically typed language, meaning that the data type of every expression is known at compile time.
Integers
There are four basic data types to store integer numbers of different sizes in Kotlin:
Note: The values assigned to each variable above are the largest allowed for the corresponding data type. This gives you an indication of their magnitude.
If you increase the assigned values even by one, the Kotlin compiler will complain because an overflow would occur at runtime (try it!).
Floating Point Numbers
Additionally, Kotlin has Float and Double to store floating point numbers up to different precision and sizes:
Here again, the assigned values are the maximum allowed for the corresponding type.
Two things to note:
- The
ein both values denotes exponentiation, for instance1e3 == 1*10^3 == 1000. - In order to denote a
Floatvalue, you have to add thefsuffix. Otherwise, Kotlin infersDoubleas the type of the number.
Text
Kotlin uses the Char type for single characters and String for arbitrary sequences of characters:
Single characters are denoted using single quotes '', whereas basic strings use double quotes "".
However, you can also use multiline strings by wrapping your string into three double quotes: """<multiline string here>""".
Booleans
Finally, Kotlin uses Boolean to store either true or false:
These two are the only valid values for the Boolean type.
Note: Type Mapping to Java
Since Kotlin transpiles to Java bytecode (if you’re using it on the JVM), it’s important to explore how language concepts translate to Java.
In contrast to Java, Kotlin has no primitive types. All types discussed in this lesson are objects at runtime. However, they do transpile to Java’s primitive types in Java bytecode:
Kotlin Type Type in Java Bytecode BytebyteShortshortIntintLonglongFloatfloatDoubledoubleCharcharBooleanbooleanThe mapping is quite trivial, but note that this mapping only works because Kotlin’s types cannot be null by default, just like Java’s primitive types cannot hold
null. Kotlin’s approach to null safety is explained in detail later in the course.
Quiz
Data Types in Kotlin
Which of these are valid data types to hold integers in Kotlin?
Byte, Short, Int, Long
Short, Int, Long, Boolean
Byte, Int, Float, Long
Exercise
Create a variable favoriteMovie that stores the title of your favorite movie and a variable rating that contains your rating for it. Ratings range from 0.5 to 5.0 in increments of 0.5.
Summary
Here’s what you should take away from this lesson:
- Kotlin has
Byte,Short,Int, andLongas basic types for integer numbers. - Kotlin uses
FloatandDoublefor floating point numbers. AFloatis denoted with a trailingf, as in17f. - Kotlin has
Charto store single characters andStringto store strings of text. - Kotlin’s basic types map to Java’s primitive types when targeting the JVM (and
Stringmaps toString).
In the following lesson, you’ll understand how and when the Kotlin compiler can infer the types used in your code.