How many new String objects will be created....
String result=s;//s is some stringfor(int i=1;i<100;i++){result=result+s;}return result;In above example how many new String objects will be created.
[201 byte] By [
jaaya] at [2007-10-2 18:49:54]

> In above example how many new String objects will be> created.Depends on the cache's content.
But usually one on each iteration.
well i asked this bcos i thought 100 objects will be created ,99 at for loop and 1 at String result=s; but some say it is just 99; and now cache's content........I am totally confused.Any link regarding this wil be helpful.Thanks in
jaaya at 2007-7-13 20:12:43 >

so no object created at String result=s;?
jaaya at 2007-7-13 20:12:43 >

> so no object created at String result=s;?No. Why?
Cache's content only matters because some of the Strings might actually already exist by coincidence.
why not....
jaaya at 2007-7-13 20:12:43 >

String one = "I am the one and only"; //there is one String object
String two = one; //there is still one String object
Variables store object references, not objects. In the second line you declare a String reference which points to the same object "one" points to, instead of initializing it with a new object.
So no object created at String result=s; indeed.
Lokoa at 2007-7-13 20:12:43 >

> why not....Do you see a "new" or concatenation operator somewhere?
ohh i got it, actually i was not aware of objects created using concatenation operator...........Thanks.so does that bring to conclusion that if string is changes then new object is created since String is not mutable?
jaaya at 2007-7-13 20:12:43 >

And what Ceci is hinting at regarding cache or string pool, is that in
String one = "I am the one and only"; //there is one String object
String two = "I am the one and only"; //there is prolly still one String object
one and two probably point to the same String object even though it looks like you are allocating two different Strings (I'm not sure if this behaviour is mandatory now or optional).
This as opposed to:
String one = new String("I am the one and only"); //there is one String object
String two = new String("I am the one and only"); //there are definitely two String objects
Yes, String is special, and this keeps things simple... ;-p
Lokoa at 2007-7-13 20:12:43 >

Thanks once again.StringBuffer result = new StringBuffer(s);//s is some stringfor(int i=1; i<100; i++){result.append(s)}return result.toString();So here only 1 object is created? since StringBuffer mutable.
jaaya at 2007-7-13 20:12:43 >

1 more thing ::since append method will call toString method by default, what is the need for result.toString.This example i found from a book
jaaya at 2007-7-13 20:12:43 >

> 1 more thing ::since append method will call toString
> method by default,
Will it? What inspired this assumption?
> what is the need for
> result.toString.
result is a StringBuffer/StringBuilder. If you want to get its value as a String you call toString.
Lokoa at 2007-7-13 20:12:43 >

> one and two probably point to the same String object> even though it looks like you are allocating two> different Strings (I'm not sure if this behaviour is> mandatory now or optional).Mandatory for all I know.
Yes for compile time string contants. String s = "I am the one and only.";String t = "I am" + " the one" + " and only.";
This must yield true:
public class a {
public static void main(String a[]) {
String s = "I am the one and only.";
String t = "I am" + " the one" + " and only.";
System.out.println(s==t);
}
}
An interesting aspect of the append() and insert() method is that the parameter may be of any type.These methods are overloaded and will perform the default conversion for the primitive types and will call the toString() method for all objects.
These are the exact wording form the book.
so i thought ...
any way it is clear now
Thanks
jaaya at 2007-7-20 23:53:04 >

Thanks BIJ001
jaaya at 2007-7-20 23:53:04 >
