Unchecked cast
An unchecked cast is a type conversion whose compatibility with the target type cannot be fully verified at compile time or runtime, often because 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 →
What to know first
- genericsGenerics are programming-language constructs parameterized by types, allowing definitions to apply to multiple concrete types while preserving type relationships.
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.
Questions about unchecked cast
What is unchecked cast?
An unchecked cast is a type conversion whose compatibility with the target type cannot be fully verified at compile time or runtime, often because type information is unavailable or erased.
Which Manning books explain unchecked cast?
Kotlin in Action, 2E and 100 Java Mistakes and How to Avoid Them.
What should I know before learning unchecked cast?
Manning books that teach unchecked cast usually introduce generics first.
What is the difference between unchecked cast and explicit conversion?
An unchecked cast is a type conversion whose compatibility with the target type cannot be fully verified at compile time or runtime, often because type information is unavailable or erased. An explicit conversion is a programmer-directed transformation of a value from one data type or representation to another.
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.

