aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing String into a method causes the method to have it's own copy, correct? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing String into a method causes the method to have it Watch "Passing String into a method causes the method to have it New topic
Author

Passing String into a method causes the method to have it's own copy, correct?

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Below is code that I modified, based on Enthuware question




Below is output of code:

Value of str in testRefs:aaabbb
Value of sb in testRefs:bbbaaabbb
s=aaa sb=bbbaaabbb



Please tell me if I understand correctly.

String is immutable, which means value will never change. 's' is String in main() and is assigned value of "aaa". When 's' gets passed to testRefs(), it is pass-by-value, as if it were an int because str gets a copy of the value of "aaa", rather than the reference, or the address of "aaa" which is why



gives 'str' from testRefs() the value of "aaabbb"

while the value of 's' from main() is still "aaa"

On the other hand StringBuffer can be changed. 'str' is StringBuffer in main() and is assigned value of "bbb". When 'str' is passed to testRefs() it is pass-by-reference such that 'sb' gets a copy of the reference value, or the address of 'str'. Hence



gives 'str' from testRefs() the value of "bbbaaabbb"

And since 'str' from testRefs() acted upon the reference address that is also pointed to by 'str' from main(), the value of 'str' from main() is also "bbbaaabbb"


Is my understanding (esp about passing Strings to methods) correct?




Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

str gets a copy of the value of "aaa"

No. Whether a class is mutable or not doesn't effect how its passed to a method. All reference variables are passed as values i.e. the address of the object they point to in the heap is passed. In this case s points to a String in the heap, so when you pass s as an argument, str receives a copy of that address not a copy of the value. Have a look at this tutorial...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Ankit Garg wrote:
str gets a copy of the value of "aaa"

No. Whether a class is mutable or not doesn't effect how its passed to a method. All reference variables are passed as values i.e. the address of the object they point to in the heap is passed. In this case s points to a String in the heap, so when you pass s as an argument, str receives a copy of that address not a copy of the value. Have a look at this tutorial...



I read and understood the tutorial. Strings is an issue that is still puzzling. So I re-wrote the code without testRefs():



If I understand:

Line 7: String str and String s both point to the same location on the heap whose value is "aaa"

Line 8: String str now points to a different location on the heap whose value is "aaabbb".



Is this correct?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Yes you are right...
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Cheers!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing String into a method causes the method to have it's own copy, correct?