SCJP 1.6版考題 166
出自 陳富國維基館
Given:
1. public class BuildStuff{
2. public static void main(String[] args){
3. Boolean test = new Boolean(true);
4. Integer x = 343;
5. Integer y = new BuildStuff().go(test, x);
6. System.out.println(y);
7. }
8. int go(Boolean b, int i){
9. if(b) return (i/7);
10. return (i/49);
11. }
12.}
What is the result?
A. 7
B. 49
C. 343
D. Compilation fails.
E. An exception is thrown at runtime.
解答
Ans: B
解說:
test是一個內含true的Boolean物件,第5行呼叫BuildStuff的物件方法go, 第9行 if(b) 會將b所指向的Boolean物件unboxing成boolean型態, 其值為true, 所以第9行的if成立,執行 return (i/7),傳回49。 Auto boxing Integer x = 343; 343是基本資料,編繹器會將此敘述自動地改為: Integer x = new Integer(343); //產生一個內含343的Integer物件 Auto un-boxing int i = x; 由於x是物件型態,編繹器會將此敘述自動地改為: int i = x.intValue(); //取得x內含的343整數值
| ||||||||||||||||||||