• 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

Garbage colln Q - help!!

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here in class J when the method m1 is called with array reference a1, is'nt the change reflected in the main method? if so, then all the objects created would be eligible for GC
but the answer says that no object will be garbage collected as only the variable a1 in the methos m1 is set to null and the change is not going to affect the a1 in the main method.
my question is when passing an array reference var to a method, is the change in that method reflected ??
class I {
private String name;
public String toString() {return name;}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {a1 = null;}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}}}
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the m1 method was setting the elements within the array to null then they would become eligible for GC...but setting the array reference itself to null doesn't make the contained elements eligible...
Remember - when passing the reference into the method, you're actually passing a COPY of the reference - changes you make to that copy (the array reference) don't impact the original (but changes you make to what the reference refers to would....)
[ March 15, 2004: Message edited by: Richard Quist ]
 
Sandhya Harish
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you.
so setting the array reference itself to null in the invoked method has no effect in the result compared to setting the elements of the array reference to some value in the invoked method. is that correct?
 
Richard Quist
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sandhya Sampath:
thank you.
so setting the array reference itself to null in the invoked method has no effect in the result compared to setting the elements of the array reference to some value in the invoked method. is that correct?


Changing the array reference in the invoked method has no impact, but if within the method you set each of the array elements to null or to some new object references then the original objects (created on lines 2, 3, and 4) become eligible for GC.
Operations you perform on the elements within the array using a copy of the array reference, have the same impact as if you had used the original array reference. It's the same as if you had declared a second array reference in your main method, assigned it the value of a1 and then began to use it to modify your array contents....
[ March 15, 2004: Message edited by: Richard Quist ]
 
Sandhya Harish
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I have got the concept right this time
 
reply
    Bookmark Topic Watch Topic
  • New Topic