Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper class array and Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper class array and Garbage Collection" Watch "Wrapper class array and Garbage Collection" New topic
Author

Wrapper class array and Garbage Collection

Nakul Kasadwala
Greenhorn

Joined: Oct 30, 2000
Posts: 22
All wrapper class is immutable.
Then what about array of wrapper class?
How many objects will be eligible for Garbage Collection in following example on the line no 6 and 7.

public class GarbageCollectionArray{
public static void main(String args[]){
trial();
}// line 7
static void trial(){
String arr[] = new String[10]; // Line 1
for(int i = 0; i < arr.length; i++){// Line 2
arr[i] = ""+i;// Line 3
}// Line 4
arr[5] = "Six";// Line 5
System.out.println("arr[5] : "+arr[5]);// line 6
}
}
Hoping for the best
asim wagan
Ranch Hand

Joined: Nov 14, 2000
Posts: 62
Hi!
1) At line 6; only one object will be available for garbage collection because arr[5] is now referencing to something else.
2) At line 7; There will be 11 objects available for gc.
Nakul Kasadwala
Greenhorn

Joined: Oct 30, 2000
Posts: 22
Any one can tell me wheather i am right or wrong?
e.g String name[] = {"NAkul","Atif","Kamlesh","MAc"};
"Assuming that name is local array there will be 4 handle on stack and 4 object on heap."
and if i say name[2] = "Jay";
"Kamlesh" will be eligible for garbage collection.
 
 
subject: Wrapper class array and Garbage Collection
 
Similar Threads
array increment
Sort the Array in reverse order
how many eligible for Garbage Collection
Garbage collection
how to use array in array?