Now that we already know what is variable, why they are important and how they can be helpful, we can understand the role of data types and how they are related to variables.
So far we know that we can have mutable and immutable variables and we can define variable by mentioning any value for it. Here is one example of two types of variables again:
val First = "Welcome to Kotlin!"
var Second = "Loving learning Kotlin!"
Val can’t be changed once declared.
And for var we learnt that we can change its value anytime later by reassigning the name to whatever we desire. At the moment we have a text “Loving learning Kotlin!” as the value for Second named variable. Lets change it to “I love learning Kotlin!”
This will be a piece of cake for you already as you can simply write the name of variable and equate it to the text string as following:
Notice that previous value of Second variable is greyed out and the last one is green which is also hinting what the output will be if program is run.
So far so good!
So we confirmed, once again, that we can change the value of var type variable any time. Lets change its value to some number say 76:
See the red line under 76? IDE is not letting us change the value of even var type variable. Surprise? You shouldn’t be!
Reason IDE is not letting us change the value of “Second” variable to 76 because its not conforming to the data type inferred by the IDE at the declaration stage.
Pay attention to inferred
Understanding Data Types
When we declared the Second variable, it was text string or simply string in nature. Though we didn’t mention it specifically, IDE did it for us automatically and now that we are trying to assign 76 as its value which is an integer thus a different data type, IDE won’t let us do it. We can confirm the reason of error by hovering our cursor over it as well:
This is probably an inbuilt data-validation that doesn’t let us mingle different data types in the same variable to keep the code and program less error prone.
But I can still store 76 as a string by enclosing it within quotation marks as following:
But remember this is NOT a real integer. Lets confirm this by declaring two more variables that are actually integers:
var Third = 100
var Fourth = 200
Now lets see what happens if we try to add Second with Third and Third with Fourth using println:
See? The Second and Third were not added together to make 176 rather were printed as 76100 which is simply two variables side by side. However, Third and Fourth being the integers were added to make 300 from 100 and 200.
So Kotlin does give due respect to each data type and these data types eventually affect and takes the effect differently of instructions run on them.
But one question still remains. Is there really no way to assign a different data type if we want to? Like the way we tried before and failed. Well… the answer to that requires more understanding.
Types of data types in Kotlin
Kotlin supports following basic data types:
Some of them we have seen and discussed whereas some are still new and require detailed discussion which we will surely have in upcoming posts.