Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Strings are immutableStrings are immutable, and passed by valuepassed by value. Every time you create a string, it will never be changed. So unlike instances of classes, you cannot modify a string by handing it to a method which modifies it.

In this case, since you return the modified String in namechanger, all you need to do is make sure you assign myname to the result of that method; like so

myname = namechanger(myname); 

Primitive types (int, float, long, etc) work this way, as do struct instances - so be sure to look for that in the future, if you're ever unsure why a struct's value is not changing when you pass it into a method.

Strings are immutable, and passed by value. Every time you create a string, it will never be changed. So unlike instances of classes, you cannot modify a string by handing it to a method which modifies it.

In this case, since you return the modified String in namechanger, all you need to do is make sure you assign myname to the result of that method; like so

myname = namechanger(myname); 

Primitive types (int, float, long, etc) work this way, as do struct instances - so be sure to look for that in the future, if you're ever unsure why a struct's value is not changing when you pass it into a method.

Strings are immutable, and passed by value. Every time you create a string, it will never be changed. So unlike instances of classes, you cannot modify a string by handing it to a method which modifies it.

In this case, since you return the modified String in namechanger, all you need to do is make sure you assign myname to the result of that method; like so

myname = namechanger(myname); 

Primitive types (int, float, long, etc) work this way, as do struct instances - so be sure to look for that in the future, if you're ever unsure why a struct's value is not changing when you pass it into a method.

Source Link
Knetic
  • 2.1k
  • 2
  • 20
  • 36

Strings are immutable, and passed by value. Every time you create a string, it will never be changed. So unlike instances of classes, you cannot modify a string by handing it to a method which modifies it.

In this case, since you return the modified String in namechanger, all you need to do is make sure you assign myname to the result of that method; like so

myname = namechanger(myname); 

Primitive types (int, float, long, etc) work this way, as do struct instances - so be sure to look for that in the future, if you're ever unsure why a struct's value is not changing when you pass it into a method.