Type casting is a process of converting one data type to another type, for example – converting int to long, long to double etc. In this tutorial we will learn how to do type conversion in Kotlin.
Type conversion in Kotlin vs Java
In java, one type is automatically converted to other type (in some cases), In Kotlin we need to explicitly convert the type.
For example:
Java:
Int is automatically converted to Long data type as long is larger than int.
// This code is valid in Java, even though we are converting int to long // because long is larger than int and it can hold integers int num1 = 101; long num2 = num1;
Kotlin:
In Kotlin the conversion is not automatic, we need to explicitly do the type conversion.
// This code is invalid in Kotlin. This will cause compile time // error, type mismatch val num1: Int = 101 val num2: Long = num1
Correct code in Kotlin:
We use the toLong() function to convert int to long in Kotlin.
val num1: Int = 101 val num2: Long = num1.toLong()
More functions for type conversion in Kotlin
In the above example, we have seen how we used toLong()
function to convert an integer to Long type. Similarly we have other functions available in Kotlin to help us in type conversion.
1. toChar() – To convert a type to Char
type.
2. toInt() – To convert a type to Int
type.
3. toLong() – To convert a type to Long
type.
4. toFloat() – To convert a type to Float
type.
5. toDouble() – To convert a type to Double
type.
6. toByte() – To convert a type to Byte
type.
7. toShort() – To convert a type to Short
type.
Simple Type Casting Examples
/** * created by Chaitanya for Beginnersbook.com */ package beginnersbook fun main(args : Array<String>){ /** * Double to int type casting */ println("4.554 to int: " + (4.554.toInt())) /** * int to Char type casting */ println("66 to Char: " + (65.toChar())) /** * Char to int type casting */ println("B to int: " + ('B'.toInt())) }
Output:
What happens when we convert a larger type to smaller type?
When converting a larger type to a smaller type, we have two possible outcomes. If the value that we are converting to a smaller type is outside the range of target type , the result is then truncated. However when the value is within the range of target type, it is converted without being truncated.
Example 1: When the value is outside the range of target type
In this example we are converting a long type to int type where the value of long type is outside the range of integer type.
fun main(args : Array<String>) { val num1: Long = 5453448989999998988 val num2: Int = num1.toInt() println("Number num1 is: $num1") println("Number num2 is: $num2") }
Output:
Number num1 is: 5453448989999998988 Number num2 is: 2041161740
Example 2: When the value is within the range of target type
In this example we are converting a long type to int type where the value of long type is within the range of integer type.
fun main(args : Array<String>) { val num1: Long = 5453448 val num2: Int = num1.toInt() println("Number num1 is: $num1") println("Number num2 is: $num2") }
Output:
Number num1 is: 5453448 Number num2 is: 5453448
Leave a Reply