Nil. Hatamova wrote:Why in the second code s1 does not change but in the first code it changes?
1.output is one two three
2.output is one two one three
But i thought output for second code will be "one two one two three";
The thing you are missing is in the second code, when s1 is passed to alter method it is treated as a local method reference variable referring to "one" , once the alter methods changes the s1 like s1 = s1 + "two" , s1 start referring to a different object on heap whereas the original s1
string remain unchanged still referring to "one"and that's why its result in this one two one three. Hope you understand.
Regards,
Kaleem