Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
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

0 votes
1 answer
asked Jan 27, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...