it’s also called Type casting , Type Conversion is to convert one data type variable into another data type
kotlin doesn’t support implicit type conversion (small to large data type). java supports In java we can assign integer values to long data types but in kotlin isn’t possible.
public class TypeConversionExample
{
public static void main(String[] args) {
int i = 12;
//implicit Typecast
long l = i; // integer value can be assigned to long data type
System.out.println(l);
}
}
In kotlin we can’t assign integer value to long data type.
fun main(args : Array<String>){
val x:Int = 34
val y:Long = x
// compile time error
//Type mismatch: inferred type is Int but Long was expected
}
Kotlin provides a helper function to explicitly convert data types.
toByte()
toShort()
toInt()
toLong()
toFLoat()
toDouble()
toChar()
val x:Int = 34
val y:Long = x.toLong()
println(120.toChar()) // x
println(11.54.toInt())// 11
println(10.toFloat()) // 10.0