Given the code fragment:

public static void main(String[] args) {
    String str = " ";
    str.trim();
    System.out.println(str.equals("") + " " + str.isEmpty());
}

What is the result?

A.

true true

B.

true false

C.

false false

D.

false true

題解

String 物件的 trim 方法可以消除開頭和結尾的所有空格,回傳新的字串,並不會改變原有字串物件的內容。所以在這題中,第 8 行執行完後,str 所參考到的字串物件依然是「 」。