Re: why this cast won't work?



On Sat, 29 Oct 2005 15:55:53 GMT, Jack <none@xxxxxxxx> wrote, quoted
or indirectly quoted someone who said :

> ServerSocket server = new ServerSocket(80);
> CastSocket cs = (CastSocket)server.accept();

Whenever you are puzzled by a run time cast exception you can write
some code like this:

Object cs = server.accept();
System.out.println( cs.getClass() );

Casting the rereference to what you are actually getting will work, an
interface it supports, or a superclass of it, but nothing else.

You can't change it into anything else with a reference cast. You
can't cast it into something it is not already.

You can with (int) somelong convert a long to an int, however.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
.



Relevant Pages

  • Re: Opinion on coding style.
    ... I don't like the way they are casting to LPCTSTR and int. ... In my opinion, if you are doing a cast, then code it as a cast, not as a ...
    (microsoft.public.vc.language)
  • Re: Opinion on coding style.
    ... I don't like the way they are casting to LPCTSTR and int. ... In my opinion, if you are doing a cast, then code it as a cast, not as a ...
    (microsoft.public.vc.language)
  • Re: Newbie: Inheritage, overloading and casting
    ... > class B extends A { ... No casting is required because every object is a descendant of Object. ... However, when getting objects back out of a Vector, you have to cast them ... A long is bigger than an int, so casting from int to long will ...
    (comp.lang.java.programmer)
  • Re: Newbie: Inheritage, overloading and casting
    ... > class B extends A { ... No casting is required because every object is a descendant of Object. ... However, when getting objects back out of a Vector, you have to cast them ... A long is bigger than an int, so casting from int to long will ...
    (comp.lang.java.help)
  • Re: Newbie question about malloc
    ... "Implicit int" no longer exists in C. ... It's generally recommended to NOT cast the return from malloc. ... %d can never be the correct format specifier for a size_t. ... C99 introduced %zu for this purpose, ...
    (comp.lang.c)