aspose file tools*
The moose likes Java in General and the fly likes Can't cast .... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can Watch "Can New topic
Author

Can't cast ....

Prashant Neginahal
Ranch Hand

Joined: Dec 04, 2002
Posts: 76
Hi All,
You can't cast any primitive array to another primitive array type even with explicit cast. But is possible for arrays of Object references.


--------------<br />Prashant<br />SCJP-91%
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
No, you can't cast that either. Have a look at the following example:
abstract class Vehicle { ... }
public class Car extends Vehicle { ... }
public class Submarine extends Vehicle { ... }
Vehicle[] v= new Vehicle[42];
Car[] carPark= (Car[])v; // suppose this works
v[0]= new Submarine(); // you've got a submarine in your carpark now ...
kind regards
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Prashant -
But for polymorphic purposes you can do the following:

So just as you might want to make polymorhic calls on objects, you can make polymorphic calls on arrays of objects.
[ December 27, 2002: Message edited by: Bert Bates ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
But you can do the following:


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Bert Bates:


Did you try this? AFAIK this should give you a runtime exception...
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
OOOOPS !!!
Sorry - but the line :
Animal [ ] aa2 = new Cat[3];
is ok.... :roll:
(jeez - some days I just shouldn't even get out of bed - and I should never post before that first cup of coffee
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Ilja Preuss:
But you can do the following:

Yes, but that's a bit besides the point because during runtime the RTTI of variable v clearly shows that v already *is* a Car[], so no cast, neither upwards or downwards needs to be applied. My example showed that downcasting arrays (if allowed) would be an infringement of the type safety which is supposed to be guaranteed, hence it is not allowed.
kind regards
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jos Horsmeier:
<hr></blockquote>
Yes, but that's a bit besides the point because during runtime the RTTI of variable v clearly shows that v already *is* a Car[], so no cast, neither upwards or downwards needs to be applied.[/QB]

But that's true for casting *any* object (in contrast to casting primitive types). In fact, casting always only affects the reference, never the object itself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't cast ....