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?
About Question enthuware.ocajp.i.v7.2.988 :
Moderator: admin
-
- Posts: 3
- Joined: Tue Apr 22, 2014 12:09 pm
- Contact:
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.988 :
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.
"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.
-
- Posts: 3
- Joined: Tue Apr 22, 2014 12:09 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.988 :
wow, that was a great example, thanks.
-
- Posts: 22
- Joined: Sun Jan 15, 2017 10:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.988 :
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?
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;
}
-
- Posts: 1
- Joined: Thu Jul 05, 2018 2:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.988 :
Code: Select all
public static void stringTest(String s){
s= s.replace('h', 's');
}
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.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.988 :
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.
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.
Who is online
Users browsing this forum: Google [Bot] and 31 guests