Its also a decouple a reference variable from an object by setting the reference variable to refer to another object.we have a following code :class GarbageProg {
public static void main(String [] args) {
StringBuffer a1 = new StringBuffer(\"hello this is my first garbage programe \");
StringBuffer a2 = new StringBuffer(\"welcome to my progarme \");
System.out.println(a1);
// At this point the StringBuffer \"hello this is my first grambage progame \" is not eligible
a1 = a2; // Redirects a1 to refer to the \"welcome to my programe \" object
// Now the StringBuffer \"hello this is my first garbage programe \" is eligible for collection
}
}
When we are created an object then we need to the method.When the method is invoked the local variable is automatically created exist only for the duration of the method.Once the method has returned, the objects created in the method are eligible for garbage collection.
By:umang
Date:2014-03-09 00:00:00
Overriding hashCode() and equals()
What It Means if we Don\'t Override equals()
Implementing an equals() Method
So What Do we Do with a Collection?
Key Interfaces and Classes of the Collections Framework
Overview of Memory Management and Garbage Collection
Overview of Java\'s Garbage Collector
When Does the Garbage Collector Run?
How Does the Garbage Collector Work?
Reassigning a Reference Variable