"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.
vinit sharma wrote:StringBuilder or StringBuffer (mutable)
now i think you got your answer???
hi, the second poster already answered your question.
Java has temporary memory to store short strings. here is example:
String a = "abc"; String b ="abc";
if (JVM temporary memory has "abc") then (a and b always refer to the same object)
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad: