I'm going to have to think aloud (and repeat back to you what you said) to make sure I've got it.
So if the array only contains Strings at the time it is passed to the method, another thread could still put other objects into it while the method that thinks it is working on a String is accessing it
Aha, that's an interesting point. And it doesn't even have to be threaded- what if I had:
I guess there's nothing reasonable that colourStrings could then be...
Note that a cast doesn't change the object.
I like that point.
So if I create the array with
then I can't cast it to String even if there are Strings (currently) inside it. If I instead create it with
then I can perform the cast fine. And if then I were to put non-Strings into the array (which the compiler thinks is OK) then I'd get a runtime ArrayStoreException.
I think I get it. But at first glance it seems so similar to casting Objects that I was stumped when it didn't work.
Thanks for all the replies!