Sie sind auf Seite 1von 2

As bzabhi said, strings are immutable in Java.

This means that a string object w


ill never change. This does not mean you can not change string variables, just t
hat you cannot change the underlying memory representation of the string. for an
example:
String str = "Hello";
str += " World!";
Following the execution of these lines, str will point to a new string in memory
. The original "Hello" string still exists in memory, but most likely it will no
t be there for long. Assuming that there are no extenuating circumstances, nothi
ng will be pointing at the original string, so it will be garbage collected.
I guess the best way to put this would be to say that when line 2 of the example
executes, a new string in memory is created from the concatenation of the origi
nal string and the string being added to it. The str variable, which is just a r
eference to a memory location, is then changed to point at the new variable that
was just created.
I am not particularly knowledgeable on the point, but, as I understand it, this
is what happens with all "non-primitive" values. Anything that at some point der
ives from Object follows these rules. Primitive values, such as ints, bools, cha
rs, floats and doubles allow the actual value in memory to be changed. So, from
this:
int num = 5;
num += 2;
the actual value in memory changes. Rather than creating a new object and changi
ng the reference, this code sample will simply change the value in memory for th
e num variable.
As for why this is true, it is simply a design decision by the makers of Java. I
'm sure someone will comment on why this was made, but that isn't something I kn
ow.
Before proceeding further with the fuss of Immutability, just take a look into S
tring class and its functionality a little before coming to conclusion about its
immutability.
This is how String works.
`String str = "knowledge"`;
This as usual, creates a string named "knowledge" and assign it a reference "str
". Simple enough? Lets perform some more functions:
String s = str ;

// assigns a new reference to the same string "knowledge"

Lets see how below statement works:


str = str.concat(" base");
This, appends a string " base" to str . But wait, how is this possible since Str
ing objects are immutable ? Well to your surprise, it is.
When the
owledge"
ings are
Object,

above statement is executed, VM takes the value of String str, i.e. "kn
and append " base", giving us the value "knowledge base". Now since Str
immutable, VM can't assign this value to str so it creates a new String
gives it a value "knowledge base" and gives it a reference str .

Important point to note here is, while the String object is immutable, its refer
ence variable is not. So that's why in above example, it was made to refer newly
formed String Object.
At this point in the example above, we have two String Objects, the first one we
created with value "knowledge", pointed to by s and second one "knowledge base"
pointed to by str. But technically we have three String Object, third one being
the literal "base" in concat statement.
Important Facts about String and Memory usage
What if we didn't have another reference s to "knowledge", we would have lost th
at String. However it still would have exist but would be considered lost due to
no reference. Look at one more example below
String s1 = "java";
s1.concat(" rules");
System.out.println("s1 refers to "+s1); // Yes, s1 still refers to "java"

Das könnte Ihnen auch gefallen