File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Sharmila Rishi
Greenhorn

Joined: Oct 11, 2006
Posts: 20
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Oct 11, 2006
Posts: 20
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

Joined: Sep 26, 2006
Posts: 9519
    
    2

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


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Oct 11, 2006
Posts: 20
Thanks Keith.



-Sharmila Rishi.
Bijendra S. Rajput
Ranch Hand

Joined: Sep 19, 2006
Posts: 41
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.


Thanks <br /> <br />Regards,<br />------------------------------<br />Bijendra S. Rajput<br />SCJP 1.5<br />------------------------------
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Oct 11, 2006
Posts: 20
Sorry for the typo.

Also the for statement should be




-Sharmila Rishi
Bijendra S. Rajput
Ranch Hand

Joined: Sep 19, 2006
Posts: 41
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

Joined: Oct 17, 2006
Posts: 26
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


"Excellency is rarely found, more rarely valued"
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
Right!


SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
 
wood burning stoves
 
subject: Garbage Collection