0 votes
1 view
in Java by (9.5k points)

Can anyone give me an easy explanation of ClassCastException?

1 Answer

0 votes
by (19.7k points)

The official document for ClassCastException says: =

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

For Example: When you try to cast an Integer to a String ClassCastException will be thrown since String is not a subclass of Integer like below: 

Object i = Integer.valueOf(42);

String s = (String)i;            // ClassCastException thrown here.

Interested in Java? Check out this Java tutorial by Intellipaat. 

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !

Categories

...