Some downcasts on arrays will fail at runtime. An object a with dynamic type A[] cannot be cast to B[], where B is a subtype of A, even if all the elements of a can be cast to B.

Ensure that the array creation expression constructs an array object of the right type.

The following example shows an assignment that throws a ClassCastException at runtime.

String[] strs = (String[])new Object[]{ "hello", "world" };

To avoid the exception, a String array should be created instead.

String[] strs = new String[]{ "hello", "world" };
  • Java Language Specification: Narrowing Reference Conversion, Subtyping among Array Types.