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?