KOTLIN - DATA TYPES
We have seen a variable is used to hold a value. Be it a number or a name or even a sentence.
var x = 5
var y = "Hello World"
But how will Kotlin come to know, if the value is a number or a name?
Well! Once again, that's the beauty of Kotlin. Kotlin makes an intelligent guess, if the value is a number or a name or a sentence or something else and create the variable accordingly.
But! Just think for a moment. Kotlin must be using some mechanism by which it can determine if the value assigned to a variable is a number or a name or a sentence or something else.
And thus 'Data Types' comes into picture.
Data Types
'Data Types' are the hidden gem that is used by Kotlin to determine if the variable is going to hold a number or a name.
But why does Kotlin need to determine if the variable it is going to hold be a number or a name? Can it not create one type of variable and store all the values there.
It cannot do that. Because if you want to store a number. It would take less space.
On the other hand, if you want to store a String like 'Hello World'. It would take a larger space.
So, whenever you want to create assign a String to a variable.
var y = "Hello World"
Kotlin makes an intelligent guess and finds out, it is a String type value and creates the storage space accordingly.
The above 'Data Type' for String is called 'str' 'Data Type'.
And similarly for a number, for a decimal value e.t.c. Kotlin has different 'Data Types'.
Note : We will keep the explanation of all the Data Types as short as possible. And will be having separate tutorials for all the Data Types.
Kotlin Data Types are categorised as :
-
String
-
Character
-
Number
-
Boolean
-
Array
Data Types used in Kotlin
-
String
As we have seen in the above example, a String is a set of letters/alphabets.
The 'Data Type' for String is 'String'.
When Kotlin finds data inside Double Quotes (""). It creates a String type variable for that value.
var y = "Hello World"
So, the 'Data Type' of a variable is determined by Kotlin and is hidden from us. Now, if we want to specify the 'Data Type' of a variable, so that it only holds string type value.
Example :
fun main(){
var x: String = "Hello World"
println(x)
}
Output :
Hello World
In the above code we have initialised the variable 'x' with the String 'Hello World'.
var x: String = "Hello World"
Now, the ':' and 'String' keyword after the variable 'x' will make sure that no values other than String would be allowed in it.
-
Character
In the above example i.e. String, we have seen how can a String variable hold a set of letters/alphabets.
Now, what if we need a variable that would just hold one letter/alphabet.
And yes, Kotlin provides that as well. i.e. A 'Char' Data Type would hold a letter/alphabet.
The only thing you need to keep in mind is, place the letter/alphabet inside single quotes ('').
Let us see in the below example,
Example :
fun main(){
var x: Char = 'Y'
println(x)
}
Output :
Y
In the above code we have initialised the variable 'x' with the letter/alphabet 'Y'.
var x: Char = 'Y'
Now, the ':' and 'Char' keyword after the variable 'x' will make sure that no values other than letter/alphabet would be allowed in it.
-
Integers
Integers are the whole numbers (i.e. 7 or 5 or 2.14).
Any whole number(Be it '-4' or '45') can be represented by the 'Int' datatype
The Int data type is used to hold whole numbers ranging from -2147483648 to 2147483647:
-
Int Data Type
The 'Data Type' for a whole number is 'Int'.
The Int data type is used to store whole numbers from -2147483648 to 2147483647
Example :
fun main(){
var x: Int = 5
println(x)
}
Output :
5
-
Long Data Type
Say, we have a large value '3372036854775808'.
Well! In this case 'Int' data type will not be able to hold that value.
A Long data type is used to store whole numbers from -9223372036854775808 to 9223372036854775808.
It is almost same as Float data type. Just that it can hold a floating point number of a larger size.
For example 3372036854775808.
Example :
fun main(){
var x: Long = 3372036854775808
println(x)
}
Output :
3372036854775808
So, in the above example, we have created a variable 'x' of type 'Long'. And assigned a large value '3372036854775808' to it.
Note : You can also use suffix 'L' with the value '3372036854775808', i.e. '3372036854775808L' to denote the value is of long data type. However, it is completely optional.
-
Byte Data Type
The 'Byte' Data Type is used to hold whole numbers between -128 to 127.
Say, there is a small number i.e. '5'. So, to save space we can use 'Byte' Data Type.
Example :
fun main(){
var x: Byte = 5
println(x)
}
Output :
5
-
Short Data Type
The 'Short' Data Type is used to hold whole numbers between -32768 to 32767.
Say, there is a number i.e. '2768'. So, to save space we can use 'Short' Data Type.
Example :
fun main(){
var x: Short = 2768
println(x)
}
Output :
2768
Note :We can still use 'Int' with numbers '5' and '2768'.
-
Float Data Type
A float data type is used to hold floating point numbers. And is used to hold the floating point numbers of 32 bit.
For example 5.987.
Example :
fun main(){
var x: Float = 5.987F
println(x)
}
Output :
5.987
So, in the above example, we have created a variable 'x' of type 'Float'. And assigned the value '5.987' to it.
var x: Float = 5.987F
Just note, we have mentioned '5.987F' instead of the number '5.987'. This is an additional step you need to write to tell Kotlin that '5.987' is a Floating point number.
-
Double Data Type
A Double data type is used to hold floating point numbers. And is used to hold the floating point numbers of 64 bit.
It is almost same as Float data type. Just that it can hold a floating point number of a larger size.
For example 5.987434343434343434343.
Example :
fun main(){
var x: Double = 5.987434862157319
println(x)
}
Output :
5.987434862157319
So, in the above example, we have created a variable 'x' of type 'Double'. And assigned the value '5.987434862157319' to it.
var x: Double = 5.987434862157319
-
Booleans
Boolean represents just two values, i.e. 'true' or 'false'.
-
Boolean Data Type
A 'Boolean' Data Type can accept only 'true' or 'false'.
Example :
fun main(){
var x: Boolean = true
var y: Boolean = false
println(x)
println(y)
}
Output :
true
false