W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/20/2021

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.

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:-

No comments:

Post a Comment

Note: only a member of this blog may post a comment.