Unchecked cast
An unchecked cast is a cast whose type compatibility cannot be fully verified because relevant type information is unavailable or erased.
Kotlin in Action, 2E
An unchecked cast is a cast whose generic type compatibility cannot be verified by the compiler or runtime because the relevant type information is unavailable.
- How it is used
- Handles generic values whose base type is known while erased type arguments cannot be verified at runtime.
- How deep it goes
- Explains erased generic checks through collection and validator failures, then develops an encapsulated generic registry pattern.
- Examples
- Demonstrates summing collections and retrieving String or Int field validators from a heterogeneous map.
- What you can do after
- Evaluate/understand when a generic cast can succeed yet later throw ClassCastException, and localize necessary unsafe casts.
100 Java Mistakes and How to Avoid Them
An unchecked cast is a cast whose compatibility cannot be fully verified by the compiler or at runtime because of erased generic type information.
- How it is used
- Warns against generic casts that silently corrupt collections and fail later during typed access.
- How deep it goes
- Explains type erasure, compiler-inserted casts, a corrupting write, and wildcard or wrapper alternatives.
- Examples
- Uses a `List<String>` cast through `List<?>` to `List<Object>`, then inserts an `Integer` before a typed read.
- What you can do after
- Evaluate/understand where unchecked generic casts defer ClassCastException and replace many with wildcards or wrappers.
Read how 100 Java Mistakes and How to Avoid Them explains unchecked cast →
Related concepts
- wildcard typeaddressed by · A wildcard type is a generic type argument representing an unknown concrete type, optionally constrained by specified bounds.
- ClassCastExceptionproduces · ClassCastException is a Java runtime exception thrown when a value cannot be cast to the requested reference type.
- type-safe APIaddresses · A type-safe API is an application programming interface whose operations, inputs, and outputs are specified and checked to prevent invalid type combinations.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

