This is in reference to this
article here's the code...
class A
{
public static void main(String[] args)
{
String one = "hey";
String two = new String("hey");
one = two = null;
String three = new String();
//String three = new String("hey");
three.intern();
System.out.println("value="+three);
}
}
this prints "value="..
second part...
class A
{
public static void main(String[] args)
{
String one = "hey";
String two = new String("hey");
one = two = null;
//String three = new String();
String three = new String("hey");
three.intern();
System.out.println("value="+three);
}
}
this prints "value=hey"....
The article mentions that even if references to String Literal & String Object are assigned to null..the String Literal Pool still has a reference to the String literal on the heap making it noneligible for garbage collection..ever!
it says.."
You can reuse String Literals with run-time Strings by utilizing the intern() method."
How can one reuse the above literal?or rather how would it be beneficial to reuse it?whats the advantage of letting a resource occupy memory eternally?
as it would help only if the string content was same?
Can anybody explain?
Thnx