Kotlin Type Conversion

In this tutorial you will learn about the Kotlin Type Conversion and its application with practical example.

Kotlin Type Conversion

In Kotlin, a value of one type will not automatically converted to another type even when the other type is larger. This is different from how Java handles numeric conversions.

Table Of Contents

Example 1:- In Java

Here, value of num1 of type int is automatically converted to type long, and assigned to num2

Example 2:- In Kotlin

In Kotlin, the size of Long is larger than Int, but it doesn’t automatically convert Int to Long. Instead, you have to explicitly convert it Long to avoid type safety.

Here is a list of functions available in Kotlin for type conversion –

toByte()
toShort()
toInt()
toLong()
toFloat()
toDouble()
toChar()

Note:- There is no conversion for Boolean types.

Example:-

Output:-

Kotlin Type Conversion

 

In this tutorial we have learn about the Kotlin Type Conversion and its application with practical example. I hope you will like this tutorial.