2

I gave a value (as variable) to a String variable.

The problem is that when I change this value, it doesn't change the other variable :

Code :

 String test = "hello";
 String myname = test;
 test = "how are you ?";

The output of myname is "hello"

I want that the output be "how are you?" the new value of test; how to do it without give the value again like :

myname = test;

I don't want to give the value again because I my code I got a lots of variables that got the test variable as value, I want to do the shortest way.

LeSam
  • 1,235
  • 4
  • 18
  • 38

5 Answers5

9

First you are assigning the value "hello" (at some place in memory) to test:

test --------------> "hello"

Then, you are setting myname to the same location in memory.

test --------------> "hello"
                  /
myname ----------/

Then, you are assigning the value "how are you?" at a new location in memory to test:

                   -> "hello"
                  /
myname ----------/

test --------------> "how are you?"

It's all about the pointers.

EDIT AFTER COMMENT

I have no idea what the rest of your code looks like, but if you want to be able to update a single String and have the rest of the references to that String update at the same time, then you need to use a StringBuilder (or StringBuffer if you need synchronization) instead:

    StringBuilder test = new StringBuilder("hello");
    StringBuilder myname = test;
    StringBuilder foo = test;
    StringBuilder bar = test;
    test.replace(0, test.length(), "how are you?");
    Assert.assertEquals("how are you?", test.toString());
    Assert.assertEquals("how are you?", myname.toString());
    Assert.assertEquals("how are you?", foo.toString());
    Assert.assertEquals("how are you?", bar.toString());

Got it from there?

MattSenter
  • 3,060
  • 18
  • 18
  • yeah Sure, but how to solve my problem – LeSam Oct 31 '13 at 00:59
  • Thanks you for you answer, I hope it will help someone, but I took the answer unfortunately delete by this owned ('fox' something) wich was about using the arrays methods. – LeSam Oct 31 '13 at 03:12
3

It sounds like you need to learn about the difference between a reference variable and the object that a variable refers to. Let's look at what your code does:

String test = "hello";

This assigns the reference variable test to refer to a constant String object with the value "hello".

 String myname = test;

Now the reference myname refers to the same String object as test does.

test = "how are you ?";

Now test refers to a new String object with the value of "how are you ?". Note that this does not change myname, as you have seen.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
3

The problem is that String is immutable so "how are you ?" is a new object. Thus myname still refers to the old object "hello".

Solution 1: Don't use two references. Why not just use test throughout your code if there's only one object?

Solution 2: Have the two references point to the same object, but use a mutable object. e.g. use StringBuilder or StringBuffer instead of String.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • the same code with a StringBuilder will have exactly the same result. – JB Nizet Oct 30 '13 at 23:00
  • For your solution 1 : I said this is just a exemple, my code does have a lot of objects ! – LeSam Oct 31 '13 at 00:53
  • THANK YOU! I spent hours thinking i forgot how pointers worked. Coming from C, I was super confused why the String/Integer classes behaved differently than my own class. Found out that's cus Strings and Integers have the "final" keyword in the class declaration, making them immutable. As you probably know in C, when you've got "int*" you can modify the value pointed by the pointer, so I assumed Java would have that behavior, since Integer objects are pointing to integers. Thank you so much. I also found out 1 of the reasons is to make those classes thread-safe. Very interesting. – Joao Apr 13 '19 at 14:29
2

That's how variables work. Variables are not references to other variables. They're references to objects. If you change what one variable references, it doesn't change what the other variabes reference.

You can change the content of an object though:

public class Container {
    private String value;
    // setter and getter omitted
}

Container test = new Container();
test.setValue("hello");
Container myname = test;
test.setValue("how are you ?);
System.out.println(myname.getValue()); // prints "how are you ?"
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks you for you answer, I hope it will help someone, but I took the answer unfortunately delete by this owned ('fox' something) wich was about using the arrays methods. – LeSam Oct 31 '13 at 03:13
2

Each of your variables is only a pointer to a real object. So after line 2, both variables point to the very same string. After line 3, test points somewhere else, and myname still points to the string.

There is no way to modify a Java String once it has been created.

To get what you want, you might use AtomicReference<String> as the data type for your variables. You don't really need the Atomic part of the reference, but it's the simplest reference type built in into Java.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • Thanks you for you answer, I hope it will help someone, but I took the answer unfortunately delete by this owned ('fox' something) wich was about using the arrays methods. – LeSam Oct 31 '13 at 03:13