Hello,
String s = new String ("abc");
1) Does the above single standalone statement create 2 objects?
2) How many total objects are created for the below two consecutive statements in a block? Is it 2 or 3?
--
String s1 ="abc";
String s1 = new String("abc");
------
As per K&B(pg. 434 SCJP6), s1=new String ("abc") will create 2 objects. One in nonpool memory and s1 will refere to it, and the 2nd object is created in the "String constant pool" memory area. I'm not sure whether 'new' always creates 2 objects or it creates 2 new objects only if the 'new String()' is preceded by another regular String assignment( in this case String s1 ="abc") having the same string value.
3) Is there any way to know of objects that are created but not assigned to any reference variable?
Thanks in advance.
Ajit
[ December 25, 2008: Message edited by: Ajit Sawant ]