Kotlin | Explicit type casting

24 Feb, 2022
In Smart Casting, we generally use is or !is an operator to check the type of variable, and the compiler automatically casts the variable to the target type, but in explicit type casting we use as operator. Explicit type casting can be done using :  
Unsafe cast operator: asSafe cast operator: as?
Unsafe cast operator: as
Safe cast operator: as?
 
Manually, we use the type cast operator as to cast a variable to target type. In the below program, variable str1 of string typecast to target type using as operator.  
Kotlin
fun main(args: Array<String>){    val str1: String = "It works fine"    val str2: String = str1 as String      // Works    println(str1)}
Output:  
It works fine
There might be possibility that we can not cast variable to target type and it throws an exception at runtime, that’s why it is called as unsafe casting. When the Integer type is used to cast to the String type, then it throws ClassCastException.  
Kotlin
fun main(args: Array<String>){    val str1: Any = 11    val str2: String = str1 as String      // throw exception    println(str1)}
Output:  
Exception in thread “main” java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String  
We can not cast a nullable string to non-nullable string, and it throws an exception TypeCastException.  
Kotlin
fun main(args: Array<String>){    val str1: String? = null    val str2: String = str1 as String      // throw exception    println(str1)}
Output: 
Exception in thread “main” kotlin.TypeCastException: null cannot be cast to non-null type kotlin.String  
Hence, we have to use target type also as nullable String so that type casting throws no exception.  
Kotlin
fun main(args: Array<String>){    val str1: String? = null    val str2: String? = str1 as String?      // throw exception    println(str1)}
Output:  
null 
 
Kotlin also provides a facility of typecasting using safe cast operator as?. If casting is not possible it returns null instead of throwing an ClassCastException exception.Here is an example, in which we are trying to cast Any type of string value that is initially known by the programmer into a nullable string then it works fine. When we initialize the Any with Integer value and try to cast into a nullable string then this typecasting is not possible and returns null to str3.  
Kotlin
fun main(args: Array<String>){     var str1: Any = "Safe casting"    val str2: String? = str1 as? String     // it works    str1 = 11    // type casting not possible so returns null to str3    val str3: String? = str1 as? String       val str4: Int? = str1 as? Int          // it works    println(str2)    println(str3)    println(str4)}
Output:  
Safe casting
null
11
 
ayushpandey3july
varshagumber28
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
How to Add Views Dynamically and Store Data in Arraylist in Android?
Retrofit with Kotlin Coroutine in Android
Content Providers in Android with Example
How to Add and Customize Back Button of Action Bar in Android?
Suspend Function In Kotlin Coroutines
Kotlin Higher-Order Functions
How to Change the Color of Status Bar in an Android App?
How to Communicate Between Fragments in Android?