But the story changes a bit for Object b here. The Explicit call to Copy constructor, forces JVM to create a regular object by new keyword on heap area and it will be placed in the String constant pool. Finally it will be assigned to the reference variable b. This process is just by passing from String constant pool management
String s = "Java";
String s = new String("Java");
What is the difference between above 2 statement ?
How many objects are created ? and how many references is created ?
Second case (String a = new String(�Java�)) just serves as a copy constructor that takes a String as an argument and creates a new instance that represents the same string of characters. Further more first string created (�Java�) is ready for garbage collection on the very next line.
The Explicit call to Copy constructor, forces JVM to create a regular object by new keyword on heap area and it will be placed in the String constant pool
String s1 = "abc";
String s2 = "ab";
s2 = s2 + "c";
3. s2 will now point to newly created String �adc�, and old content is ready to be garbage collected.
But still i wondered by tracing output of code given by Nishant in 4th row Reply(Above).
after the statement s2=s1+"c"; why compiler does not check for existing String in String Constant Pool ??
1. I agree that due to new keyword it is created in heap, But i never heard or read the process of moving object from heap to Pool ???
I too failed to find any Heap to String Pool movement. But if it is not so, does it mean that String are also created outside the Pool?
But i think(though i don't know whether it is correct or not ?) there nothing like moving objects from heap to pool.