Reference K&B page 360 Ch6: java.lang According to Kathy: String s=new String("abc"); //will result in two objects - one on the heap and //the other("abc") in the string pool. Well my Question is that if after the above statement we have: String s1="abc"; Then will a new object be created on the pool or the reference of object already in the pool will be assigned to S1.
Also if we originally had : String s="abc"; String s2=new String("abc");//2 then how many objects will be created on line 2?
When the compiler sees a String literal like "abc" the first time, it gets interned and placed on the String constants pool. If you afterwards create a new String object like String str=new String("abc"); only one object is created on the heap. However, should "abc" literal not have been encountered before, it would have been interned first, so you can say that 2 objects were created.