ClassCastException

Discussion in 'Java' started by hon123456, Jan 17, 2006.

  1. hon123456

    hon123456 Guest

    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.
     
    hon123456, Jan 17, 2006
    #1
    1. Advertising

  2. hon123456

    Aj-India Guest

    try this...

    System.out.println(x.toString());
     
    Aj-India, Jan 17, 2006
    #2
    1. Advertising

  3. hon123456 wrote:
    > 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
    --
    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/
     
    Thomas Weidenfeller, Jan 17, 2006
    #3
  4. hon123456

    Guest

    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

    hon123456 wrote:
    > 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.
     
    , Jan 17, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luther Baker

    ClassCastException and ClassLoaders

    Luther Baker, Sep 9, 2003, in forum: Java
    Replies:
    3
    Views:
    854
    Luther Baker
    Sep 10, 2003
  2. Richard A. DeVenezia
    Replies:
    2
    Views:
    603
    Andrew Thompson
    Jan 8, 2004
  3. Robert Paris
    Replies:
    2
    Views:
    1,186
    Anton Spaans
    Feb 17, 2004
  4. Bob Dubery
    Replies:
    5
    Views:
    586
    Bob Dubery
    Jun 19, 2004
  5. Affan
    Replies:
    0
    Views:
    445
    Affan
    Jun 24, 2004
Loading...

Share This Page