• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper class array and Garbage Collection

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Something about .... going for a swim. With this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic