• 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 Collection

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
Please consider the following code....



This is a question from WhizLabs.
Question:How many objects will be eligible for garbage collection before System.gc() is executed?

The given answer is 3.

I thought it was none since we only pass a reference of array object to the method f(array)and also since array variable in f() method is local to that method, it doesnt affect the array var in the main method.


Could anyone shed some light on it?

Thanks in advance.

-Sharmila Rishi.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reference to the array itself is sent by value, but inside the method you can change the individual entries in the array using the formal parameter.
 
Sharmila Rishi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith.
So the statements


are infact changing the values of the array....

while the statement

is just setting the local variable to null.

Am I right..?


-Sharmila Rishi.
[ October 17, 2006: Message edited by: Sharmila Rishi ]
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Referring to the above code, how many objects are actually created? I guess that there are 8 objects created alltogether?

MyClass[] array=new MyClass[4]; //4 here and

for(int i=0;i<arra.length;i++)
array=new MyClass(); // 4 here since the for loop iterates 4 times.

Please anyone can tell on this?

Regards,
Jothi Shankar Kumar. S
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sharmila Rishi:
Thanks Keith.
So the statements


are infact changing the values of the array....

while the statement

is just setting the local variable to null.

Am I right..?


-Sharmila Rishi.

[ October 17, 2006: Message edited by: Sharmila Rishi ]



That's right.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
Hi All,

Referring to the above code, how many objects are actually created? I guess that there are 8 objects created alltogether?

MyClass[] array=new MyClass[4]; //4 here and

for(int i=0;i<arra.length;i++)
array=new MyClass(); // 4 here since the for loop iterates 4 times.

Please anyone can tell on this?

Regards,
Jothi Shankar Kumar. S



In this line

,

only one object is created, the array itself. Initially each reference in the array is null.
 
Sharmila Rishi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith.



-Sharmila Rishi.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for(int i=0;i<arra.length;i++)
array=new MyClass(); // error


When I am compiling this program, I am getting a error.

found : MyClass
required: MyClass[]
array = new MyClass();

and when I was compiling with MyClass[], then I am getting a error that array size is missing.

So, I am confused what will be the asnwer of this question.

Thanks,

Bijendra R.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bijendra S. Rajput:
for(int i=0;i<arra.length;i++)
array=new MyClass(); // error


When I am compiling this program, I am getting a error.

found : MyClass
required: MyClass[]
array = new MyClass();

and when I was compiling with MyClass[], then I am getting a error that array size is missing.

So, I am confused what will be the asnwer of this question.

Thanks,

Bijendra R.



The original posted intended for the code to be

 
Sharmila Rishi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the typo.

Also the for statement should be




-Sharmila Rishi
 
Bijendra S. Rajput
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith and Sharmila

Please give me a one more favour.

array = new MyClass();

// What we are doing in this line actually


Bijendra R.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Considering the modified code, I think total 5 objects are created.

MyClass[] array=new MyClass[4]; //1 array object(with 4 references)

for(int i=0;i<array.length;i++)
array[i]=new MyClass(); // 4 MyClass objects.

Please correct me if I am wrong.

- Ramu
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right!
reply
    Bookmark Topic Watch Topic
  • New Topic