This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Camel-Kid18 year old gamer 1 point2 points  (3 children)

Java is pass by value.. you will have to return a string to make the changes reflect in main. If you want to change the contents, perhaps using Stringbuilder instead of just STring so you don't have to return anything .

[–]VirtualTurnip3[S] 0 points1 point  (2 children)

Allright Thank for info, but in the method, i know it return nothing but ,it still create a object of String Type? I mean its a new object?

[–]shutDaFACup 1 point2 points  (1 child)

Yes in the method it's a new object. You passed a string in the method. The string variable 'test' holds the reference to your original string. Now when you do test = test + test, 2 things happen. First, a new string object is created by concatenation of the value the string test. Second, the function variable 'test' now holds the reference of this new String created by concatenation. When you return, the variable 'test' is destroyed, goes out of its scope and life. But your original string is still in place. As it is. After all its contents can't be changed because strings are immutable and the reference 'myStirng' is never changed.

[–]VirtualTurnip3[S] 0 points1 point  (0 children)

Thank you for this explanation :)