What is the result of the following code?
public class Main { public void s(String s1, StringBuilder s2) { s1.concat("!!!"); s2.append("!!!"); }/*from w w w . j a v a 2s. c o m*/ public static void main(String[] args) { String s1 = "s"; StringBuilder s2 = new StringBuilder("s"); new Main().s(s1, s2); System.out.println(s1 + " " + s2); } }
B.
A String is immutable.
Calling concat()
returns a new String but does not change the original.
A StringBuilder is mutable.
Calling append()
adds characters to the existing character sequence along with returning a reference to the same object.