Step-by-Step Explanation: Kotlin Type Casts
Kotlin provides high-level constructs for checking and converting types safely, combining strict compile-time checks with automatic type conversions.
1. Type Checks with is and !is
The is operator checks if an expression matches a specific type at runtime. Its negative counterpart !is checks if it does not match.
if (obj is String) {
// obj is checked as String
}
2. Smart Casts
Smart casting is the Kotlin compiler's ability to automatically cast a variable to a specific type after a type check has been performed, avoiding manual or redundant castings.
- if Condition Scopes: Once checked, the variable changes type inside the if body.
- Logical Conjunction (&&): The variable is smart cast on the right-hand side if checked on the left.
- when Expressions: In each branch matching a type check, the variable is smart cast automatically.
3. Unsafe Cast Operator (as)
The as operator executes an explicit, forced conversion to a specific target type.
- If the runtime instance type is incompatible with the target type, a ClassCastException is thrown.
- To handle potential null values, you must explicitly cast to a nullable type target (as String?).
4. Safe Cast Operator (as?)
To prevent unexpected crashes from failed casts, Kotlin provides the as? operator.
- If the cast succeeds, it returns the cast value.
- If the cast fails or the instance is null, it gracefully returns null instead of throwing an exception.
5. Generics Type Erasure and Star Projections
Due to JVM type erasure, generic type arguments are only checked at compile time and are not preserved at runtime.
- You cannot check if an object is a List<String> directly at runtime using is List<String>.
- Instead, you must use a star projection check (is List<*>) to verify it is a List, followed by an unchecked cast (as List<String>).
Comments
Post a Comment