SCJP題庫第162題

Given:
11. public static void main(String[] args){
12.   Integer i = new Integer(1) + new Integer(2);
13.   switch(i){
14.     case 3: System.out.println("three"); break;
15.     default: System.out.println("other"); break;
16.     }
17. }
What is the result?


A. three
B. other
C. An exception is thrown at runtime.
D. Compilation fails because of an error on line 12.
E. Compilation fails because of an error on line 13.
F. Compilation fails because of an error on line 15.
 
Ans: A
 
解說:
swtich(x)這個x必須為整數,代入i物件是否可以?請參考auto-boxingauto-unboxing,整數可以自動打包成整數型態,同樣的整數型態也是自動拆包成整數

Comments

Popular posts from this blog

Android-使用webview在V3版的Google地圖GPS定位