Have few doubts in String,
I may be wrong with certain statements as i am writing this based on my understanding from various articles on Internet, please bear with me.
When we do String str1 = new String ("newStr1");. This creates 2 string objects. One in regular heap and another in string pool. Why 2 objects and their usage? Why not just one on pool?
If we create object as String str2 = new String("newStr2").intern();. This checks if a similar (meaningfully equal) object is there in pool, give a reference to it. If none, it creates one in pool but not in heap? If so then we should use intern most of the times to save memory? Though it would impact performance a bit. So basically it's String str2 = "newStr2"; (interning is implicit for string literals)
After Java-6 the string pool moved from perm gen space to heap area? So basically we have now just one area as heap or does string pool sits as a separate section in heap now? If it's not a separate section then still 2 objects are created?