Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

ClassCastException

Thread view: 
hon123456 - 17 Jan 2006 05:34 GMT
Dear all,
         I am trying to learn Java. I found the explanation of
ClassCastException in Sun website as follows. But it seems rather
simple. I don't understand " cast an object to a subclass of which it
is not an instance" in the explanation. Please help me. By the way,
please suggest any good website that explains the ClassCastException in

detail.

>From sun:

Thrown to indicate that the code has attempted to cast an object to a
subclass of which it is not an instance. For example, the following
code generates a ClassCastException:

    Object x = new Integer(0);
    System.out.println((String)x);

Thanks.
Aj-India - 17 Jan 2006 07:10 GMT
try this...

System.out.println(x.toString());
Thomas Weidenfeller - 17 Jan 2006 07:49 GMT
> I am trying to learn Java. I found the explanation of
> ClassCastException in Sun website as follows. But it seems rather
> simple.

Yes it is.

> I don't understand " cast an object to a subclass of which it
> is not an instance" in the explanation.

There is nothing more to say. Do you know what an object is? Do you know
what a subclass is? Do you know what an instance is? If not, get a good
beginners textbook and start to read. Look for the section close to the
start of the book where they talk about objects and types.

> Please help me. By the way,
> please suggest any good website that explains the ClassCastException in
> detail.

A web site explaining class cast exceptions? You are making an affair
out of something which is nothing.

>      Object x = new Integer(0);
>      System.out.println((String)x);

An Integer is not a String. Please, get a textbook.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

dp_ganatra@rediffmail.com - 17 Jan 2006 11:53 GMT
Hi,

This exception is thrown when attempting to cast an object to a class
that it cannot be cast to.

Class cast exception is thrown when an attempt is made to cast an
object which is not of the appropriate runtime type.  If u see the
class hierarchy, it will look like ...

java.lang.object
        java.lang.Throwable
              java.lang.Exception
                         java.lang.RuntimeException
                                 java.lang.ClassCastException

You can also visit :
http://www.cs.duke.edu/csed/ap/subset/doc/ap/java/lang/ClassCastException.html

You can visit

> Dear all,
>           I am trying to learn Java. I found the explanation of
[quoted text clipped - 15 lines]
>
> Thanks.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.