What is Kotlin Type Conversion? | Kotlin Tutorial
What is Kotlin Type Conversion? | Type conversion
In this article, we will discuss
- What is the type conversion?
- Types of type conversion
- Examples of type conversion
What is type conversion?
The type conversion is a process in which the variable of one data type is converted into another. For example, converting the variable of long data type into an int data type.
Types of type conversion:
Two types of type conversion exist in the programming world. They are
- Explicit type conversion
- Implicit type conversion
Explicit type conversion
It means converting the large data type into the smaller data type.
Example:
convert long type variable into an int type.
Implicit type conversion
It means converting the small data type into the large data type.
Example:
convert int type variable into a long type.
But kotlin only supports explicit type conversion. It means an int value can not be assigned to the long data type. However, Java supports implicit type conversion.
Example of type conversion:
var num = 100
var myNum: Long = num.toLong()
There are so many helper functions available that are used in the conversion from one data type to another data type:
- toByte()
- toShort()
- toInt()
- toLong()
- toFLoat()
- toDouble()
- toChar()
Type conversion is one of the most important topics to learn in the kotlin programming language.