ClassCastException in Java
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
ClassCastException happens when you try to cast an object to a type it isn't actually an instance of at runtime.
The key thing to understand is the difference between the reference type and the actual object type. If I write Object obj equals "Hello", the reference type is Object but the actual object in memory is a String. Now if I try to cast that to Integer, like Integer num equals (Integer) obj, the compiler allows it because obj is declared as Object, which could theoretically hold an Integer.
But at runtime, the JVM checks the actual type and sees it's a String, not an Integer, so it throws ClassCastException. The way to prevent this is using the instanceof operator before casting. You write if (obj instanceof String) and only cast inside that block.
Modern Java makes this even nicer with pattern matching. You can write if (obj instanceof String str) and the variable str is automatically created as a String, so no manual cast needed. The biggest improvement came with generics in Java 5.
When you use List of String instead of a raw List, the compiler enforces that only Strings go into that list. When you call get, you get a String directly with no casting required. All the type checking happens at compile time, so you can't get a ClassCastException from that collection.
Freshers often get confused because they think the compiler checks everything about types. It doesn't. The compiler only checks what it can see from the declared types.
At runtime, the actual object might be something different, which is when ClassCastException happens. That's why modern Java emphasizes generics — they let the compiler verify more at compile time so you avoid runtime surprises.
ClassCastException in Java
Direct Answer
ClassCastException occurs at runtime when you try to cast an object to a type it's not actually an instance of. For example, casting a String stored in an Object reference to Integer fails because String is not an Integer subclass.
Code Example
public class CastingExample {
public static void main(String[] args) {
// Safe upcasting: always works (String IS-A Object)
Object obj = "Hello";
// Unsafe downcasting: compiler allows but runtime may fail
// Integer num = (Integer) obj; // ClassCastException!
// SAFE: check type before casting
if (obj instanceof String str) { // Java 16+ pattern matching
System.out.println("Length: " + str.length());
}
// Generics prevent ClassCastException at compile time
List<String> names = List.of("Alice", "Bob");
String first = names.get(0); // no cast needed, type-safe
// Legacy code without generics (pre-Java 5 style)
List rawList = new ArrayList();
rawList.add("text");
rawList.add(42);
String item = (String) rawList.get(1); // ClassCastException: Integer can't be cast to String
}
}
Common Mistakes & Pitfalls
- Assuming reference type equals actual type: Freshers see Object obj = "Hello" and think obj is an Object, forgetting that the actual instance is a String. The reference type is just what the variable can hold, not what's actually there.
- Casting without instanceof check: Directly casting without verifying the type first leads to runtime crashes. Always check instanceof before downcasting in production code.
- Using raw collections: Not using generics and relying on manual casting everywhere is error-prone and was necessary before Java 5 but is now considered bad practice.
Check your understanding
Answer these without looking back at the article.
- What is the instanceof operator in Java?
- How do generics prevent ClassCastException?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.