我们为什么要施放到同一类型?

时间:2022-03-24 18:58:20

can anyone explain why am i getting incompatible type error and should cast to Object[] if ref[0] is already object array(class [Ljava.lang.Object;)

任何人都可以解释为什么我得到不兼容的类型错误,如果ref [0]已经是对象数组(类[Ljava.lang.Object;),应该转换为Object []

public class Test {
    public static void main(String args[]) {
        Object[] ref = new Object[1];
        Object[] x = new Object[]{1};
        while (true) {
            ref[0] = new Object[]{ref};
            System.out.println(ref[0].getClass()); //class [Ljava.lang.Object;
            ref = ref[0]; // incompatible type error
        }

    }
}

1 个解决方案

#1


3  

At compile stage, ref[0]'s data type is Object, ref's data type is Object[].

在编译阶段,ref [0]的数据类型是Object,ref的数据类型是Object []。

Object and Object[] are not the same type.

Object和Object []的类型不同。

#1


3  

At compile stage, ref[0]'s data type is Object, ref's data type is Object[].

在编译阶段,ref [0]的数据类型是Object,ref的数据类型是Object []。

Object and Object[] are not the same type.

Object和Object []的类型不同。