About Question enthuware.ocajp.i.v7.2.1182 :
Posted: Thu Aug 23, 2012 2:26 pm
Hi Guys,
I'm after running this question a couple of times step by step and just don't understand why null isn't the answer,
s=aaa sb=null
where as the correct answer is s=aaa sb=bbbaaabbb
Its a great question and wondering if you can point me in the direction of some text explaining why Setting the local reference str and sb (in method testRefs()) to null, does not affect the variables s and sb of the main() method
Thanks
Code: Select all
public class TestClass{
public void testRefs(String str, StringBuilder sb){
str = str + sb.toString();
sb.append(str);
str = null;
sb = null;
[b] System.out.println("s="+str+" sb="+sb);// prints out s=null and sb=null[/b]
}
public static void main(String[] args){
String s = "aaa";
StringBuilder sb = new StringBuilder("bbb");
new TestClass().testRefs(s, sb);
System.out.println("s="+s+" sb="+sb);
}
}
s=aaa sb=null
where as the correct answer is s=aaa sb=bbbaaabbb
Its a great question and wondering if you can point me in the direction of some text explaining why Setting the local reference str and sb (in method testRefs()) to null, does not affect the variables s and sb of the main() method
Thanks