Given:
11. public static void main(String[] args) {
12. Object obj = new int{1, 2, 3};
13. int[] someArray = (int[])obj;
14. for (int i: someArray) System.out.print(i + " ");
15. }
What is the result?
A. 1 2 3
B. Compilation fails because of an error in line 12.
C. Compilation fails because of an error in line 13.
D. Compilation fails because of an error in line 14.
E. A ClassCastException is thrown at runtime.
答案:A
參考:7-8 物件的轉型
------------------------------
12行 , 陣列是物建的一種所以可以轉型為Object.
13, obj可以強迫轉型為原型int陣列.
14, for each時把someArray列印出來,結果為1 2 3.
沒有留言:
張貼留言