Problem Statement
Explain type casting in Kotlin including smart casts, safe casts, and unsafe casts.
Explanation
Kotlin provides smart casts that automatically cast types after is checks eliminating explicit casts, so after if x is String you can use x dot length directly without casting. Use safe cast operator as question mark that returns null if cast fails rather than throwing ClassCastException, useful when you're unsure if object is the target type like val str equals obj as question mark String. Unsafe cast operator as throws ClassCastException if cast fails, use only when certain of the type like val str equals obj as String. Smart casts work in if expressions, when expressions, and while loops, but require the variable to be immutable val or local var to ensure type safety. The compiler tracks type information through control flow, smart casting in the appropriate branches, and you can use is for type checks, not is for negative checks, and combine with when expressions for elegant type-based branching. Understanding these casting mechanisms helps write type-safe code that's both concise and robust.
