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

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

    As you say: String inheriting from Object and int not inheriting from Object, that's the reason. int, boolean, double... are primitive types and they don't extend from Object. You should use Integer instead of int.

    Integer[] t;
    Object[] z = (Object[]) t;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-21 09:58

    I just found the answer I was looking for myself. The reason why you cannot cast int[] to Object[] is not because int is a primitive and does not extend Object, but because int[] itself does not extend Object[]. In code:

    int[] t = new int[0];
    Object ot = t;
    System.out.println(ot instanceof Object[]);
    // --> prints 'false'
    String[] s = new String[0];
    Object os = s;
    System.out.println(os instanceof Object[]);
    // --> prints 'true'
    

    Edit: the boxing is necessary because Eclipse knows that int[] and Object[] are incompatible.

    Edit II: Btw this if(obj instanceof Object[]) allows to check wether a boxed array is an array of a primitive type.

    0 讨论(0)
  • 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[].

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-21 10:17

    According to Java Spec 4.10.3 Subtyping among Array Types:

    • If S and T are both reference types, then S[] >1 T[] iff S >1 T.

    • Object >1 Object[]

    • If P is a primitive type, then Object >1 P[]

    S >1 T means that T is a direct subtype of S

    It basically means, that int[] and Integer[] are in different branches of type hierarchy in Java and can't be cast one to another.

    0 讨论(0)
提交回复
热议问题