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

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

Moderator: admin

Post Reply
madman0817
Posts: 3
Joined: Tue Apr 22, 2014 12:09 pm
Contact:

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

Post by madman0817 »

I am probably wrong here, and am not good with wording, but, sb is declared in main, and 's' in stringBuilderTest is 'declared' in it's method, so, how is the method able to change the variable outside of it's local declaration? Is this sending a variable by reference? So confused.. I would think that you should need something like sb = stringBuilderTest(sb);


edit: Meaning, sb is an instance variable, not a member variable, right?

Help?

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

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

Post by admin »

sb is indeed a local variable. But the objects created using new are always created on "heap", which is a common memory area for the whole program. So when you pass sb to stringBuilderTest, the address to the same StringBuilder object is passed to the method and that method operates on the same instance.

"instance variable" and "member variable" are two names for the same thing. "sb" is neither of them. sb is a local variable, also known as "automatic variable".

You should go through http://www.javaranch.com/campfire/StoryCups.jsp and then http://www.javaranch.com/campfire/StoryPassBy.jsp for a clear understanding.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

madman0817
Posts: 3
Joined: Tue Apr 22, 2014 12:09 pm
Contact:

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

Post by madman0817 »

wow, that was a great example, thanks.

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

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

Post by jamesmccreary »

I believe since we are passing in a reference variable instead of a primitive variable, the same "object" within the main method and the stringBuilderTest method is being manipulated by the stringBuilderTest method, yes?

Would therefore the following two versions of stringBuilderTest be equivalent?

Code: Select all

//Original
public static void stringBuilderTest(StringBuilder s) {
     s.append("o");
} 

//My modification
public static StringBuilder stringBuilderTest(StringBuilder s) {
     StringBuilder sbToReturn = s.append("o");
     return sbToReturn;
} 

mgb9987
Posts: 1
Joined: Thu Jul 05, 2018 2:38 pm
Contact:

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

Post by mgb9987 »

Code: Select all

public static void stringTest(String s){
       s= s.replace('h', 's');
         }
why s=s.replace() is same as s.replace() here.
I tried this code. But o/p is same for both. Why???

In stringTest() method "s=s.....()" changed the value of the String but not in main() method.

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

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

Post by admin »

Two things:

1. Strings are immutable. So, replace doesn't change the original string. It creates a new one with the changes.
2. Java uses "pass by value" for passing parameters and returning results. This is a fundamental concept and you need to read about it from a good book (or online articles) to understand why assigning a new value to a method parameter doesn't affect the original parameter that was passed to the method.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 31 guests