Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • 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?



 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right...
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic