[英]Why should we cast to the same type?
誰能解釋為什么我遇到不兼容的類型錯誤,並且如果ref[0]
已經是對象數組,則應該將其class [Ljava.lang.Object;
為Object[]
( 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
}
}
}
在編譯階段, ref[0]
的數據類型為Object
, ref
的數據類型為Object[]
。
Object
和Object[]
不是同一類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.