This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
posted
0
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: 8712
posted
0
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 ]
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
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
posted
0
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: 8712
posted
0
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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.