String str=new String ("xyz");
I read somewhere JVM create 2 object 1st in pool and 2nd in heap.
Is this true ? If true then why JVM create 2 object when one is already there can anyone explain?
String str=new String ("xyz");
I read somewhere JVM create 2 object 1st in pool and 2nd in heap.
Is this true ? If true then why JVM create 2 object when one is already there can anyone explain?
Yes, you are right. It creates two objects. One in String Constant Pool and another object in Heap pointing to the String pool.
For further reference, please check the following discussion: String s = new String("xyz"). How many objects has been made after this line of code execute?
"xyz" is in the constant pool once the class has been loaded str is put on the heap when that line actually runs
see Where does Java's String constant pool live, the heap or the stack?