String str1="JAVA"; String str2="JAVA"; String str3=new String("JAVA"); String str4=new String("JAVA").intern();
2 objects will be created. str1 and str2 refer to the same object due to the concept of a string literal pool and str3 points to a new object because using the new operator and str4 points to the same points of the object on str1 and str2 because intern() checks the string pool for a string having the same meaning.
str1=str2=str3=str4=null;
One object will be eligible for GC. This is an object created using String str3=new String("JAVA") . The first String object is always accessible through a link stored in a string literal pool.
Is my explanation correct?
Jaikant bhagwan das
source share