------------------------------------------------
Question: Given the following sequence of
Java statements
1. StringBuffer sb = new StringBuffer("abc");
2.
String s = new String("abc");
3. sb.append("def");
4. s.append("def");
5. sb.insert(1, "zzz");
6. s.concat(sb);
7. s.trim();
Which of the following statements are true:
A. The compiler would generate an error for line 1.
B. The compiler would generate an error for line 2.
C. The compiler would generate an error for line 3.
D. The compiler would generate an error for line 4.
E. The compiler would generate an error for line 5.
F. The compiler would generate an error for line 6.
G. The compiler would generate an error for line 7.
-----------------------------------------------
The answer is D F.
I understand that a String as an object is immutable. It is not right to apply append() and concat() to the String. Then I am confused, why the trim() method can be used to the String without generating an error, which is answer G?
Since the String class have several methods (such as concat(string), substring(int), trim), under what conditions can those methods be used to modify the String?
Please help! :roll: Thank you in advance.
--Moya