I want to send a string to a method and change the string there.
The method should return void.
Example:
String s = "Hello";
ChangeString(s);
String res = s;
//res = "HelloWorld"
-------------------------------------------
private void ChageString(String s){
s = s + "World";
}
How can I do it in Java?
Can it be done without adding another class?
Thanks!
PB
Your method cannot work with the current interface because of two reasons:
sin your method it only modifies the locals, not the originalsin the calling code.To make your method work you need to change the interface. The simplest change is to return a new string:
Then call it like this:
See it working online: ideone