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

后端 未结 6 1367
长情又很酷
长情又很酷 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 10:12

    Any array, including int[] is actually an Object. This is why you can cast to Object. However, int is a primitive, so it doesn't extend Object, so you cannot cast to Object[].

提交回复
热议问题