about questions from ocajp.i.v8.2.1284
Posted: Fri Jul 14, 2017 2:37 am
Hi,
I don't understand why output is "javajavac" for below question :
I understand String is immutable. That is why its value can't be change only by calling method and StringBuilder can change its value by calling method. However, return type for replaceStringBuilder method is void. That is why I thought it doesn't change the value of s2 but it seems the answer is saying it changed the value for s2
I don't understand why output is "javajavac" for below question :
Code: Select all
What will the following class print when run?
public class Sample{
public static void main(String[] args) {
String s1 = new String("java");
StringBuilder s2 = new StringBuilder("java");
replaceString(s1);
replaceStringBuilder(s2);
System.out.println(s1 + s2);
}
static void replaceString(String s) {
s = s.replace('j', 'l');
}
static void replaceStringBuilder(StringBuilder s) {
s.append("c");
}
}