Jatin sachdev wrote:As i am doing s1=s1+" "+s1 then it should have changed the original string but it is not....
Why should it have changed the original String? Strings are immutable and cannot be changed. What you're doing in that method is creating new Strings that you assign to the local parameter variables
s1 and
s2. Afterwards, these two no longer refer to the original Strings but to the new Strings. The original Strings, with references in the original
s1 and
s2 local variables of the main method, remain as they were.
If you use StringBuilder instead of String, this allows you to modify the actual object instead of only the reference. It will then work as you expected: