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 所参考到的字符串对象依然是「 」。
