2009年2月6日 星期五

第41題

1. public class KungFu{
2. public static void main(String[] args){
3. Integer x = 400;
4. Integer y = x;
5. x++;
6. StringBuilder sb1 = new StringBuilder("123");
7. StringBuilder sb2 = sb1;
8. sb1.append("5");
9. System.out.println((x == y) + " " + (sb1 == sb2));
10. }
11. }
What is the result?
A. true true
B. false true
C. true false
D. false false
E. Compilation fails.
F. An exception is thrown at runtime.
----------------------------------------------------------------------------------
答案:B
參考:6-2 Java方法、11-2 文字類型
物件參照到觀念

1. public class KungFu{
2. public static void main(String[] args){
3. Integer x = 400;
4. Integer y = x;
5. x++; //x=x+1 此時y =400,x=new Integer(401)
6. StringBuilder sb1 = new StringBuilder("123");
7. StringBuilder sb2 = sb1;
8. sb1.append("5");
9. System.out.println((x == y) + " " + (sb1 == sb2));
10. }
11. }

沒有留言:

張貼留言