Given:
public class String1 {
public static void main(String[] args) {
String s = "123";
if (s.length() > 2) {
s.concat("456");
}
for (int x = 0; x < 3; x++) {
s += "x";
}
System.out.println(s);
}
}
What is the result?
A.
123
B.
123xxx
C.
123456
D.
123456xxx
E. Compilation fails
题解
程序第 6 行,使用字符串对象的「concat」方法会回传字符串连接后的结果,但并不会改变对象本身所表示的字符串。因此这题只会在字符串「123」后面串上 3 个「x」。
