'Java string value change in function

I have this very awkward question...

void changeString(String str){
    str = "Hello world":
}

main(){
    String myStr = new String("");
    changeString(myStr);
}

When main returns, the value is still "" and not "Hello world". Why is that?

Also, how do I make it work? Let's say I want my function changeString to change the string it got to "Hello world".



Solution 1:[1]

Java uses a call by value startegy for evaluating calls.

That is, the value is copied to str, so if you assign to str that doesn't change the original value.

Solution 2:[2]

If the changing of your String happens very often you could also assign a StringBuffer or StringBuilder to your variable and change its contents and only convert it to a String when this is needed.

Solution 3:[3]

Expanding a bit on NullUserException's excellent answer, here's a more general solution:

public class Changeable<T> {
   T value;

   public Changeable(T value) {
      this.value = value;
   }

   public String toString() {
      return value.toString();
   }

   public boolean equals(Object other) {
      if (other instanceof Changeable) {
         return value.equals(((Changeable)other).value);
      } else {
         return value.equals(other);
      }
   }

   public int hashCode() {
      return value.hashCode();
   }
}

Yura's original code can then be rewritten as:

void changeString(Changeable<String> str){
   str.value = "Hello world":
}

void main() {
   Changeable<String> myStr = new Changeable<String>("");
   changeString(myStr);
}

And, just for fun, here it is in Scala:

class Changeable[T](var self: T) extends Proxy;

object Application {
   def changeString(str: Changeable[String]): Unit = {
      str.self = "Hello world";
   }

   def main(): Unit = {
      val myStr = new Changeable("");
      changeString(myStr);
   }
}

Solution 4:[4]

Because the reference myStr is passed by value to the function changeString and the change is not reflected back to the calling function.

P.S : I am not a Java guy.

Solution 5:[5]

Bill, I have a solution to your problem which uses a List as a pointer in java!

void changeString(List<String> strPointer ){
    String str = "Hello world";
    strPointer.add(0, str);
}

main(){
    LinkedList<String> list = new LinkedList<String>();
    String myStr = new String("");
    changeString(list);
    myStr = list.get(0);
    System.out.println( myStr );
}

This answer takes a little extra work to insert and get out the string from the list, however the final line will print "Hello world!"

I hope this can help others as well!

-Port Forward Podcast

Solution 6:[6]

Here's the one more solution by StringBuffer/StringBuilder worked for me.

static void changeValue(StringBuilder str){
    str.append("newValue");
}

main(){
    StringBuilder originalVal= new StringBuilder();
    changeValue(originalVal);
    System.out.println(originalVal.toString());
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 starblue
Solution 2 Johannes Wachter
Solution 3 Community
Solution 4
Solution 5 benathon
Solution 6 K.Andy Wang