If string is a immutable class then how come it is allowing us to make changes ?
-
Hi all, I am having one small doubt. If string is a immutable class then how come it is allow us change ? ex: String s1="abc"; s1="def"; Sop(s1); it gives op as :- def. does it mean we have made changes on string object ? please tell me. Thanks ************ S G KORE *******************
-
Hi all, I am having one small doubt. If string is a immutable class then how come it is allow us change ? ex: String s1="abc"; s1="def"; Sop(s1); it gives op as :- def. does it mean we have made changes on string object ? please tell me. Thanks ************ S G KORE *******************
"abc" and "def" are strings. s1 is not a string, it is a variable holding a reference to a string. at first it refers to "abc", later on it refers to "def". The string "abc" never got modified, it may still be alive (kept alive by another reference, as in the example below), or it is dead and garbage collectible.
string s1="abc";
string s2=s1;
s1="def";
Sop(s1); // refers to "def"
Sop(s2); // refers to "abc":)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
"abc" and "def" are strings. s1 is not a string, it is a variable holding a reference to a string. at first it refers to "abc", later on it refers to "def". The string "abc" never got modified, it may still be alive (kept alive by another reference, as in the example below), or it is dead and garbage collectible.
string s1="abc";
string s2=s1;
s1="def";
Sop(s1); // refers to "def"
Sop(s2); // refers to "abc":)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Thank you Luc Pattyn. Now I got clear picture . :) ************ S G KORE *******************
-
Thank you Luc Pattyn. Now I got clear picture . :) ************ S G KORE *******************
In addition to the above comment, what I suggest you is that try to draw a picture in a paper while you work on Object types & references so that you will always know what you are doing and have the right picture in your mind. It is very helpful, even in Arrays and Memory Allocations