1 Object obj = "String"; 2 String str = (String) obj; // OK 3 4 Object[] arr = new String[] { "ab", "cd" }; 5 String[] arr2 = (String[]) arr; // OK 6 7 Object[] objs = {"One", "two"}; 8 String[] strs = (String[]) objs; // java.lang.ClassCastException异常,Why?
对于第8行代码报错的情况不理解,为什么报错。
我现有的认识是,objs的每个元素引用的不是String对象吗,按1、2行的代码来讲,再强制转换回来应该是可以的。
请指教,感恩!!!
莫回无
FFIVE
相关分类