Why can you cast int[] to Object, but not to Object[]?

后端 未结 6 1362
长情又很酷
长情又很酷 2020-12-21 09:52

So this works:

int i;
Object a  = (Object) i;
int[] t;
Object b = (Object) t;
String[] s;
Object[] t = (Object[]) s;

But this does not:

6条回答
  •  时光取名叫无心
    2020-12-21 09:57

    An object is a class instance or an array.

    It is stated in The JLS section 4.3.1.

    Now, int[] is an array, which is an Object.

    String[] s; 
    

    and

    int[]
    

    differ in following way:

    Former can point to an array of String objects, but latter can point to an array of primitive int.

提交回复
热议问题