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

后端 未结 6 1379
长情又很酷
长情又很酷 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:13

    A int[] is an array of primitives but also an Object itself. It is not an array of Objects

    There is no auto-boxing support for arrays. You need to pick the right type of array to start with and not be converting it at runtime.

提交回复
热议问题