"Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
"
This is the description given in JavaDoc by Sun.
I wish to know what this sharing refers to.
Thanks in advance.
I cannt draw diagram here but i try .. for further refrence you can see chapter 6 of K&B
String class(Immutable)
1.String x="abc";
x.concat("def");
System.out.println("x= "+x);
//output is x= abc
/*Because no new assingment was made,
the new string ojbect created with the concat()
method was abandoned insatantly*/
2.String x="abc";
x=x.concat("def");
System.out.println("x= "+x);
//output is x= abcdef
/*we got a nice ne string out of the deal, but the
downsideis that the old string has been lost in string pool,
thus wasting memory*/
It doesn't matter if you win by an inch or a mile; winning's winning.