简体   繁体   中英

Why should we cast to the same type?

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; )

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
        }

    }
}

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

Object and Object[] are not the same type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM