Exception in thread “main” java.lang.ClassCastException

Posted: May 9, 2012 in Java

ClassCastException:

It is the child class of Runtime Exception and it is unchecked. It is raised automatically by JVM. When ever we are trying to perform type casting from parent to child type.

Example:

public class Test {

public static void main(String[] args) {

String s =”gopal”;

Object o = (Object)s;

Object object = new Object();

String string = (String) object;  ——> Here we are type casting Parent to child so we get ClassCastException

}}

 

Output:

Exception in thread “main” java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String

at com.clarity.test1.main(test1.java:97)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s