Kotlin Data Types
During the previous lesson we discussed about declaring variables in Kotlin.During this lesson let’s discuss about Kotlin data types.
As you learnt this is a perfectly working variable declaration in Kotlin .
We don’t have to explicitly declare the data type of the variable, but Kotlin automatically defines the data type by considering the initial value of the variable.
Even though it is possible to omit the data type from the declaration, you have also given the option of specifying the data type after the variable name. Like this
var subject : String = «Maths»
Insert a colon after the variable name, then declare the type of the variable.
What is the benefit of explicitly declaring data type ?
If there is no data type declaration, you have to initialize the variable. Because Kotlin need to see the initial value to identify the data types. So this code will compile.
But here we haven’t given an initial value to the varible “subject” . As a result of that, Kolin is unable to recognize the data type. Therefore this will not compile.
But If we declare the data type explicitly, this will compile perfectly.
Java primitive data types
In Java we have two types of variables. Primitives and object references.
As you all know there are 8 primitive data types in Java.
Java uses wrappers to make those primitive data types behave like objects. If we want to create Objects for those types, we can use corresponding wrapper classes.
Data | Java Primitive Type | Related Wrapper Class |
---|---|---|
8 bit integers (numbers from -128 to 127) | byte | Byte |
16 bit integers (numbers from -32768 to 32767) | short | Short |
32 bit integers (numbers from -2147483648 to 2147483647) | int | Integer |
64 bit integers (numbers from -9223372036854775808 to 9223372036854775807) | long | Long |
true , false | boolean | Boolean |
32 bit floating point numbers(floating point numbers from 3.4e−038 to 3.4e+038) | float | Float |
64 bit floating point numbers(floating point numbers from 1.7e−308 to 1.7e+308) | double | Double |
single character | char | Character |
All types in Kotlin are objects
But in Kotlin, there is no such distinction as primitives and object references. All types in Kotlin are objects. So, fortunately we don’t have to deal with two things such as primitive data types and wrapper classes. We only have Kotlin objects for data types.
Data | Kotlin Object |
---|---|
8 bit integers (numbers from -128 to 127) | Byte |
16 bit integers (numbers from -32768 to 32767) | Short |
32 bit integers (numbers from -2147483648 to 2147483647) | Int |
64 bit integers (numbers from -9223372036854775808 to 9223372036854775807) | Long |
true , false | Boolean |
32 bit floating point numbers(floating point numbers from 3.4e−038 to 3.4e+038) | Float |
64 bit floating point numbers(floating point numbers from 1.7e−308 to 1.7e+308) | Double |
single character | Char |
Byte, Short, Int and Long for integral types. We have Float and Double for floating point types.Then we have Boolean for true or false. And finally for characters we have Char.
Use suffixes for Long and Float.
Just like in Java, we have to use the suffix ‘L’ to specify a Long value. Let’s define a long variable as distance.
var distance : Long = 456712363345L
Also , we have to use the suffix ‘F’ to specify a Float value. Let’s define a float variable as temperature.
var temperature : Float = 56.7F
var temperature : Float = 56.7f
For float values we can use either captal F and simple f.
But for Long values we can only use capital L.
And when we are using Char data type that character should be written within single quotes.
Like this. var MyCharacter : Char = ‘A’
If you write a character within double quotes, that will be recognized as a string.
And , before end this lessons There is one last thing I should mention .In Kotlin ,it is highly recommended to declare variables with the val keyword unless there is a requirement to change its value later. Because it ensures that our variables cannot be modified by other threads unexpectedly.
We can explicitly define a data type by inserting a colon after the variable name and then declaring the type .
Basic values in Kotlin
This is a chapter from the book Kotlin Essentials. You can find it on LeanPub.
Every language needs a convenient way to represent basic kinds of values, like numbers or characters. All languages need to have built-in types and literals. Types are used to represent certain types of values. Some type examples are Int , Boolean , or String . Literals are built-in notations that are used to create instances. Some literal examples are a string literal, which is text in quotation marks, or an integer literal, which is a bare number.
In this chapter, we’ll learn about the basic Kotlin types and their literals:
There is also the array primitive type in Kotlin, which will be covered in the chapter Collections.
In Kotlin, all values are considered objects (there are no primitive types), so they all have methods, and their types can be used as generic type arguments (this will be covered later). Types that represent numbers, booleans, and characters might be optimized by the Kotlin compiler and used as primitives, but this optimization does not affect Kotlin developers, therefore you don’t need to even think about it.
Let’s start discussing the basic types in Kotlin, one by one.
Numbers
In Kotlin, there is a range of different types that are used to represent numbers. They can be divided into those representing integer numbers (without decimal points) and those representing floating-point numbers (with decimal points). In these groups, the difference is in the number of bits used to represent these numbers, which determines the possible number size and precision.
To represent integer numbers, we use Int , Long , Byte , and Short .
Type | Size (bits) | Min value | Max value |
---|---|---|---|
Byte | 8 | -128 | 127 |
Short | 16 | -32768 | 32767 |
Int | 32 | − 2 31 -2^ − 2 3 1 | 2 31 − 1 2^ — 1 2 3 1 − 1 |
Long | 64 | − 2 63 -2^ − 2 6 3 | 2 63 − 1 2^ — 1 2 6 3 − 1 |
To represent floating-point numbers, we use Float and Double .
A plain number without a decimal point is interpreted as an Int . A plain number with a decimal point is interpreted as a Double .
You can create Long by using the L suffix after the number. Long is also used for number literals that are too big for Int .
Similarly, you can create a Float by ending a number with the F or f suffix.
There is no suffix to create Byte or Short types. However, a number explicitly typed as one of these types will create an instance of this type. This also works for Long .
This is not a conversion! Kotlin does not support implicit type conversion, so you cannot use Byte or Long where Int is expected.
If we need to explicitly convert one number to another type, we use explicit conversion functions like toInt or toLong .
Underscores in numbers
In number literals, we can use the underscore _ between digits. This character is ignored, but we sometimes use it to format long numbers for better readability.
Other numeral systems
To define a number using the hexadecimal numeral system, start it with 0x . To define a number using the binary numeral system, start it with 0b . The octal numeral system is not supported.
Number and conversion functions
All basic types that represent numbers are a subtype of the Number type.
The Number type specifies transformation functions: from the current number to any other basic type representing a number.
This means that for each basic number you can transform it into a different basic number using the to function. Such functions are known as conversion functions.
Operations on numbers
Numbers in Kotlin support the basic mathematical operations: