liveBook · Manning

ClassCastException

ClassCastException is a Java runtime exception thrown when a value cannot be cast to the requested reference type.

How 2 Manning books explain it

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.

Read how Kotlin in Action, 2E explains ClassCastException →

Related concepts

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.