Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1182 :

Posted: Thu Aug 23, 2012 2:26 pm
by JohnnyFanta
Hi Guys,

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);

  }
}
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

Re: About Question enthuware.ocajp.i.v7.2.1182 :

Posted: Thu Aug 23, 2012 3:19 pm
by admin
Remember that, in Java, everything is pass by value.

Here is a very good article that explains what it means: http://www.javaranch.com/campfire/StoryPassBy.jsp

Once you understand this, the question is actually very easy and you will know why the answer is not null.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1182 :

Posted: Thu Mar 27, 2014 11:44 am
by __Bill
^ That link is the only clear explanation I've seen of this. sb.append(str) affects the object because your copy of the reference (the remote control) is manipulating the object. But sb = null is simply smashing your copy of the remote.

But it has to be borne in mind that String is a special case and that neither str = null or str = str.concat(astring) would affect the orignal string.

Re: About Question enthuware.ocajp.i.v7.2.1182 :

Posted: Fri Apr 11, 2014 4:07 pm
by JeramieH
A "remote control", what an awesome tangible way to think of references.

Re: About Question enthuware.ocajp.i.v7.2.1182 :

Posted: Mon Nov 24, 2014 3:00 am
by gparLondon
WoW!!!!! what an outstanding explanation.... Liked it very much.

Re: About Question enthuware.ocajp.i.v7.2.1182 :

Posted: Fri Jun 26, 2015 11:21 am
by evafang2008
Great post! After I read the article about 'pass by value', it really helps for this question! Now I completely understand why the result for sb is 'bbbaaabbb', not null!
sb in main() and sb in TestClass() are different reference variables, but they refer to the same object StringBuilder(). sb.append(str); changed the object which affected both sb in main() and sb in TestClass(). However sb = null, this only changed sb in TestClass(), not sb in main().

That's my understand, hope it can help others.

Thanks,

Eva

Re: About Question enthuware.ocajp.i.v7.2.1182 :

Posted: Tue Aug 18, 2020 3:48 pm
by Dreamweaver
This one was really hard. That is my interpretation:

sb = null - destroy the remote controller but the TV is still there