SCJP題庫第041題


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.

Ans:B

解說:
Integer x = 400; è (auto boxing) Integer x = new Integer(400);
Integer y = x; à (auto boxing) Integer y = new Integer (x); //Integer的建構子要代入的是整數值,因此,xunboxing成整數 à Integer (400)
x++ à x.add(1);
xy應看成二個整數變數,不是物件參考
String是不可變的字串物件
Ex.
String s = “ABC”; s.concat(“D”);
此時會產生一個新字串物件ABCD,只是,s還是指向舊的字串物件ABC
StringBuilderStringBuffer物件其內含的字串是可變的
Ex.
StringBuilder s = new StringBuilder("ABC");
s.append("D");
s所指向的字串物件內容由ABC變為ABCD

Comments

Popular posts from this blog

Android+Google Map API v3 Geocoding(地址轉經緯度度