无错误
Object[] a = new String[]{"12","34","56"};
String[] b = (String[]) a;
没有错误
Object a = new String[]{"12","34","56"};
String[] b = (String[]) a;
运行时错误:ClassCastException
Object[] a = new Object[3];
a[0] = "12";
a[1] = "34";
a[2] = "56";
String[] b = (String[]) a;
运行时错误:ClassCastException
Object[] a = {"12","34","56"};
String[] b = (String[]) a;
当然,如果一个String[]
是作为Object[]
创建的,我们可以将它向下转换为Object[]
。我的问题是,当
Object[]
是作为Object[]
创建的,但其中所有成员都是字符串时,为什么我们不能将其转换为String[]
?这是因为安全原因还是只是没有实现这个功能呢?