posted 21 years ago
It doesn't matter what the Object[] is containing - if it was created using new Object[], it really is an Object[] array even if all the elements are Strings. No Amount of casting can change this. However, if the array is really a String[] (created using new String[] for example) and you're just using a reference variable of type Object[] to refer to it, then you can cast. E.g.will compile OK, but will throw a ClassCastException at runtime because the array is not a String[] - just an Object[] containing Strings. However this works:
This illustrates why the cast in the previous example was allowed to succeed at compile time - a reference which is declared to be of type Object[] could hold a reference to a String[] (as it does in the second example). So the cast must be allowed at compile time - but it can still be invalid at run time.
This may seem strange, but it's nearly identical to the waycompiles but does not run, and compiles and runs.
[ May 30, 2002: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister