Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't cast ....

 
Prashant Neginahal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you can do the following:
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bert Bates:


Did you try this? AFAIK this should give you a runtime exception...
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic