• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?



 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right...
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic