0

From the oracle doc

String is immutable also Strings are constant; their values cannot be changed after they are created. and because they are immutable they can be shared. String buffers support mutable strings.

but I can always do the following:

String name="SO";

name="SE"; 

I can change the value so how can it be immutable and it is said that for security reason also like database connectivity etc..

Pardon me for asking such basic question but I need to understand.

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
Joe
  • 4,460
  • 19
  • 60
  • 106
  • Okay dude, if as per all of your answer if JVM creates new object when the value to a variable changes, is it not costly, it can very well update the existing reference's value instead of creating new object and occupying unnecessary space in the memory?. sorry this kind of question. – Joe Dec 05 '13 at 10:11

5 Answers5

4

name="SE" by doing this, you are changing the value of name variable, not the String object SO itself. String are immutable in the sense, String object SO can't be modified once created. By doing name = name+"TEST"; there will be a new String object SOTEST created in the memory, not existing String object SO will be modified.

For further details look here and here. There are lot of example and explanation.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
2

Yes, you can change the string your name variable points to. But let's see what happens when you execute the following code sequence:

String name = "SO"; //line 1
name = "SE"; //line 2

At line 1: a new String object is created, that holds the value "SO";

At line 2: a new String object is created, that holds the value "SE"; your name variable value changes, in that it points to another reference, which is the second String object; the first String object ("SO") is still on the heap, but is not referenced anymore and is made available for future garbage collections, if any.

What you have to understand here is that, as soon as the String object containing the char sequence {'S', 'O'} is constructed, the char sequence wrapped by that String object could never change again. E.g. you cannot make that object wrap the char sequence {'S', 'E'}. That's what immutability is all about.

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36
1

In the example you provide you have created two Strings. First a String SO is created and the reference is assigned to name. Then a new String is created, SE and that reference is assigned to name. You never actually modified the first String that was created.

        String temp = "SO";
        String name = temp;
        name = "SE";
        System.out.println(temp.equals("SO")); //prints true;
        System.out.println(temp == name); //compares references prints false
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

When you change value of your String reference, you in fact create new object.And your reference now is linked to this new object.

String myValue = "old"; -> VM creates String object "old"
myValue = "new"; -> VM created String object "new"; "old" object still exists but any reference is linked to this, so we can say : "old" is lost

k4sia
  • 414
  • 1
  • 6
  • 18
1

See for example the below image from the internet:

enter image description here

The big cloud is the heap where objects are being stored, s is the reference to the object (in your question you are using name as the reference)

When you do s = "abcd" a new object is created in the heap, and s is a reference to it. (Like the top arrow shows)

The important bit:

When you then do something like s = "abcdef" or s = s + "ef" the immutable string object "abcd" cannot be changed and so a new object is created and s loses it's reference to the old string (shown by dotted line) and now references the new String (Shown by the bottom arrow).

The old object I would assume is picked up by Garbage Collector at some point.

Levenal
  • 3,796
  • 3
  • 24
  • 29