0 votes
in Kotlin by
Explain Kotlin Type Conversion or Kotlin Type Casting

1 Answer

0 votes
by

Type Conversion is a procedure of converting one type of data variable into another data type variable. It is enormously, also known as Type Casting.

Eminently, in Java, implicit type of type conversion or implicit type of typecasting of a smaller data type to larger data type is supported.

For Example : int abc = 15;
Long bcd = abc; // compiles successfully

However, in kotlin, implicit conversion of a smaller data type to a larger data type is not at all supported that is int data type cannot be converted into long data type implicitly.

For Example : var abc = 15
Val bcd : Long = abc // error

However, In Kotlin, type conversion is done explicitly. Here comes the guidance of helper functions that guides the conversion of one data type variable to another.

There are certain helper function which are pre – owned for the conversion of one data type to another :

toInt()

toByte()

toShort()

toChar()

toLong()

toFloat()

toDouble()

For Example : var abc = 15
Val bcd : Long = abc.toLong() // compiles successfully
...