ClassCastException
ClassCastException is a Java runtime exception thrown when a value cannot be cast to the requested reference type.
100 Java Mistakes and How to Avoid Them
ClassCastException is the Java runtime exception thrown when a value cannot be cast to the requested reference type, including when identically named classes come from different class loaders.
- How it is used
- Prevents fragile runtime conversions at API boundaries and in generic collections.
- How deep it goes
- Explains explicit checks and compiler-inserted generic casts down to runtime type erasure, while deferring pattern details to Java 16 references.
- Examples
- Uses Object-to-String processing, equals() casting, pattern instanceof, and a corrupted List<String> whose failure is delayed.
- What you can do after
- Diagnose/debug with it by locating unsafe explicit or erased generic conversions and replacing them with checked type contracts.
Read how 100 Java Mistakes and How to Avoid Them explains ClassCastException →
Kotlin in Action, 2E
ClassCastException is a JVM runtime exception thrown when a value is used as an incompatible type.
- How it is used
- Explains why unchecked generic casts can defer type failures until values are consumed.
- How deep it goes
- Traces a cast from compiler warning through successful runtime check to failure during numeric summation.
- Examples
- Uses printSum with integer lists, integer sets, and string lists to contrast successful output and two exception paths.
- What you can do after
- Diagnose/debug unchecked generic casts by distinguishing failures at the cast boundary from failures during value use.
Related concepts
- data conversionproduced by · Data conversion is the transformation of a value or data from one type, format, or representation to another, explicitly or implicitly.
- as operatorproduced by · The as operator is language syntax that associates a value or expression with a specified type through assertion, casting, or conversion, depending on the language.
- exact-match accuracyproduced by · Exact-match accuracy evaluates outputs by whether they exactly equal their references under specified comparison rules.
- implicit conversionproduced by · Implicit conversion is an automatic change of a value from one type to another according to language rules, without an explicit conversion operation in the source code.
- raw typeproduced by · A raw type is a Java generic type used without specifying its type arguments, weakening compile-time type checking for that use.
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.

