y mashalkar

Greenhorn
+ Follow
since Dec 20, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by y mashalkar

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
Hi,
I still did not get your point...

i tried this one...

class A
{
static void b(Object obj)
{
System.out.println("Object");
}
static void b(A obj)
{
System.out.println("String");
}
public static void main(String[] args)
{
b(null);
}
}

it did compile...and printed the same answer "String"
How?