Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Sharmila Rishi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Joe Harry
Ranch Hand
Posts: 10091
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 2409
  • Mark post as helpful
  • send pies
  • 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: 2409
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Keith.



-Sharmila Rishi.
 
Bijendra S. Rajput
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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: 2409
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Ramu Malur S R
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic