Kotlin | Explicit type casting

In Smart Casting, we generally use is or !is operator to check the type of variable, and 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 :

  1. Unsafe cast operator: as
  2. Safe cast operator: as?

Unsafe cast operator : as

Manually, we use type cast operator as to cast a variable to target type.
In below program, variable str1 of string type cast to target type using as operator.



filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>){
    val str1: String = "It works fine"
    val str2: String = str1 as String      // Works
    println(str1)
}

chevron_right


Output:

It works fine

There might be possiblility that we can not cast variable to target type and it throws an exception at runtime, thats why it is called as unsafe casting.
When Integer type is used to cast to String type, then it throws ClassCastException.

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>){
    val str1: Any = 11
    val str2: String = str1 as String      // throw exception
    println(str1)
}

chevron_right


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-nullabe string, and it throws an exception TypeCastException.

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>){
    val str1: String? = null
    val str2: String = str1 as String      // throw exception
    println(str1)
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>){
    val str1: String? = null
    val str2: String? = str1 as String?      // throw exception
    println(str1)
}

chevron_right


Output:

null 

Safe cast operator: as?

Kotlin also provides a facility of type casting 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 which is initially known by programmer into nullable string then it works fine. When we initialize the Any with Integer value and try to cast into nullable string then this type casting is not possible and returns null to str3.

filter_none

edit
close

play_arrow

link
brightness_4
code

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)
}

chevron_right


Output:

Safe casting
null
11


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.