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]
# 1
> In above example how many new String objects will be> created.Depends on the cache's content.
CeciNEstPasUnProgrammeura at 2007-7-13 20:12:43 > top of Java-index,Java Essentials,New To Java...
# 2
But usually one on each iteration.
CeciNEstPasUnProgrammeura at 2007-7-13 20:12:43 > top of Java-index,Java Essentials,New To Java...
# 3
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 > top of Java-index,Java Essentials,New To Java...
# 4
so no object created at String result=s;?
jaaya at 2007-7-13 20:12:43 > top of Java-index,Java Essentials,New To Java...
# 5
> so no object created at String result=s;?No. Why?
CeciNEstPasUnProgrammeura at 2007-7-13 20:12:43 > top of Java-index,Java Essentials,New To Java...
# 6
Cache's content only matters because some of the Strings might actually already exist by coincidence.
CeciNEstPasUnProgrammeura at 2007-7-13 20:12:43 > top of Java-index,Java Essentials,New To Java...
# 7
why not....
jaaya at 2007-7-13 20:12:43 > top of Java-index,Java Essentials,New To Java...
# 8

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 > top of Java-index,Java Essentials,New To Java...
# 9
> why not....Do you see a "new" or concatenation operator somewhere?
CeciNEstPasUnProgrammeura at 2007-7-13 20:12:43 > top of Java-index,Java Essentials,New To Java...
# 10
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 > top of Java-index,Java Essentials,New To Java...
# 11

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 > top of Java-index,Java Essentials,New To Java...
# 12
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 > top of Java-index,Java Essentials,New To Java...
# 13
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 > top of Java-index,Java Essentials,New To Java...
# 14

> 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 > top of Java-index,Java Essentials,New To Java...
# 15
> 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.
CeciNEstPasUnProgrammeura at 2007-7-20 23:53:04 > top of Java-index,Java Essentials,New To Java...
# 16
Yes for compile time string contants. String s = "I am the one and only.";String t = "I am" + " the one" + " and only.";
BIJ001a at 2007-7-20 23:53:04 > top of Java-index,Java Essentials,New To Java...
# 17

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);

}

}

BIJ001a at 2007-7-20 23:53:04 > top of Java-index,Java Essentials,New To Java...
# 18

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 > top of Java-index,Java Essentials,New To Java...
# 19
Thanks BIJ001
jaaya at 2007-7-20 23:53:04 > top of Java-index,Java Essentials,New To Java...