![]() ![]() Example 1: When the value is outside the range of target type However when the value is within the range of target type, it is converted without being truncated. If the value that we are converting to a smaller type is outside the range of target type, the result is then truncated. When converting a larger type to a smaller type, we have two possible outcomes. What happens when we convert a larger type to smaller type? toShort() – To convert a type to Short type. toByte() – To convert a type to Byte type.ħ. toDouble() – To convert a type to Double type.Ħ. toFloat() – To convert a type to Float type.ĥ. ![]() toLong() – To convert a type to Long type.Ĥ. toInt() – To convert a type to Int type.ģ. toChar() – To convert a type to Char type.Ģ. Similarly we have other functions available in Kotlin to help us in type conversion.ġ. In the above example, we have seen how we used toLong() function to convert an integer to Long type. Val num2: Long = num1.toLong() More functions for type conversion in Kotlin We use the toLong() function to convert int to long in Kotlin. In Kotlin the conversion is not automatic, we need to explicitly do the type conversion. because long is larger than int and it can hold integers ![]() This code is valid in Java, even though we are converting int to long Int is automatically converted to Long data type as long is larger than int. In java, one type is automatically converted to other type (in some cases), In Kotlin we need to explicitly convert the type. In this tutorial we will learn how to do type conversion in Kotlin. ![]() Type casting is a process of converting one data type to another type, for example – converting int to long, long to double etc. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |