2

I have a small question

I have created this: String abc="abc"; String xyz=abc;

if (xyz==abc){
 System.out.println("true");
}

And the output is "true" as expected because the Strings, Objects, are point to the same memory position, right? But, if I have this:

String abc="abc";
String xyz=abc;
abc = "acvb";
System.out.println(abc + " " + xyz);
if (xyz==abc){
 System.out.println("true");
}

The output is "false" (it doesn't appear). Does this mean that changing the value of a String create a new memory position and points towards it, the String that was changed, (unlike the arrays)? Thanks

drf
  • 8,461
  • 32
  • 50
Diogo Santos
  • 780
  • 4
  • 17

3 Answers3

3

Does this mean that changing the value of a String create a new memory position and points towards it,

The String abc is a reference to an object. The reference can be changed in this case, and you are pointing it to a new object when you use =. However when you say

the String that was changed,

You have to be clear and distinguish that only the reference to the String has changed. None of the String objects have changed, they are immutable.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

Perhaps some pictures will make it clear, although my reliance on ASCII art to do the pictures may reduce the clarity somewhat...

Any time you have an object, a variable (or parameter) of that object really holds a "reference" or "pointer" to the object, not the object's value. A String is an object in Java, but this answer really applies to any kind of object. I've sometimes explained it this way: Think of an object as a house, and a reference is a piece of paper with the address of the house written on it. If you make a copy of the reference, you're creating a new piece of paper, but it has the same address written on it. So there's still just one house.

So here's the situation after each line of code:

String abc="abc";

variable abc 
     |
     |
     v
---------------
|    abc      |   (String object.)
--------------- 

Now:

String xyz=abc;

You're copying the reference, so now you have two variables pointing to the same object:

variable abc   variable xyz 
     |               |
     |     +---------+
     v     v
---------------
|    abc      |   (String object.)
--------------- 

These two variables point to the same object, but they are still two separate variables. So if you change the variable abc, it has no effect on xyz, which still is a reference to the same object. That is, after this:

abc = "acvb";

               variable xyz        variable abc
                     |                  |
           +---------+                  |
           v                            v
---------------                  ----------------
|    abc      |                  |    acvb      |
---------------                  ----------------

So now, abc==xyz is false because they're not pointing at the same object.

ajb
  • 31,309
  • 3
  • 58
  • 84
2

The important thing to understand is that a String is an object and when you use == to compare object references you are comparing addresses, not values. If you have two String references aString and bString and compare them with bool theyAreEqual = aString == bString;, theyAreEqual will be true only if aString and bString are referencing the exact same String object.

If aString and bString reference two different String objects with identical (or different) contents then the result will be false. (And when you say something like abc = "acvb"; you have assigned a new object to the reference abc.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151