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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
JohnnyFanta
Posts: 2
Joined: Thu Aug 23, 2012 2:03 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10061
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

__Bill
Posts: 25
Joined: Thu Mar 27, 2014 11:35 am
Contact:

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

Post 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.

JeramieH
Posts: 22
Joined: Wed Jan 08, 2014 11:24 am
Contact:

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

Post by JeramieH »

A "remote control", what an awesome tangible way to think of references.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

WoW!!!!! what an outstanding explanation.... Liked it very much.

evafang2008
Posts: 9
Joined: Fri Jun 26, 2015 11:15 am
Contact:

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

Post 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

Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

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

Post by Dreamweaver »

This one was really hard. That is my interpretation:

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

Post Reply

Who is online

Users browsing this forum: No registered users and 108 guests