|
Re: Immutable String help
|
Posted: Nov 12, 2003 11:23 AM
|
|
When you write:
String str = "hello";
then there are two things that you are dealing with. One is the variable str. Here, str is a reference variable, which can hold a reference to any String object. Note that I am saying str is a "variable". You can always change the value of str because it is a variable. By string immutability it means the string object that is constructed is immutable. So the real string object that hold the value "Hello" is immutable and not the reference variable str (There is more inside to this explanation).
Let us take a real world example, say, human being is immutable. Let us say we have a Human class and you are one of its instances. Syntactically we write it as:
Human Guido = your_physical_being;
You can understand that Guido is just your name and not the real human being object we are referring to. Here, your_physical_being is immutable and we cannot change it. However, we can start calling another human as Guido any time. It is just a name. So, when we say,
Guido = kishori_physical_being;
Here, I took your name and give that name to myself. Did it change anything to you. No. Now, by Guido it is me who is recognized and not you. So, what happens to you now. If you have another name then you may still be recorgnized. If not, then get ready to be garbage collected.
|
|