It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan exam doubt 19 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 "Dan exam doubt 19" Watch "Dan exam doubt 19" New topic

Dan exam doubt 19

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hii in one question dan

class I {
private String name;
protected void finalize() {System.out.print(name);}
public I(String s) {name = s;}
class J {
private static void m1(I[] a1) {
a1[0] = a1[1] = a1[2] = 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

After method m1 returns, the object created on which line is not eligible for garbage collection?

a. 1
b. 2
c. 3
d. 4
e. None of the above
f. Compile-time error
g. Run-time error

the answer is "a"
with expanation...
Please note that this question asks which objects are NOT eligible for garbage collection after method m1 returns. After method m1 returns, the array a1 created on line 1 is not eligible for garbage collection. Method m1 sets all elements of the array to null; so the objects created on lines 2, 3 and 4 are eligible for garbage collection when method m1 returns.

i didn't understand why the elements of a[0], a[1], a[2] become null...when there refernce in main method is still there ?

Thanks and Regards, Amit Taneja
Bajji Pat
Ranch Hand

Joined: Apr 05, 2005
Posts: 50
Hi Amit,

In your case, there is one "Array Object" created on the heap which can hold four references(or whatever the number).
In this case the elements of the array have been nulled but the array object has itslef not been nulled.

This code is similiar to your case :

Balaji Pattabhiraman
[ April 22, 2005: Message edited by: Bajji Pat ]
Sunyaluk Boonmas

Joined: Apr 18, 2005
Posts: 24
This is the sample of pass by reference problem.

a1 in main method and a1 in argument has the same reference.

So, if we change any attribute of them, it will effect each other value.

However array object in line 1 still reachble by live thread.

SCJP Passed(93%)<br />SCWCD Preparing<br />SCEA future.<br /> <br />Here is the great community!!
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
i m still not clear Sir

so can you please tell me what is pass by refernce ?
is it like this

class A{
Stringbuffer a = new Stringbuffer("abc");
Stringbuffer b;

public static void main(String args[])
System.out.println(a +" "+b);

will produce answer abcxyz abcxyz

because both refer to same object ?

is this is fine eg of "pass by reference" ?
Sunyaluk Boonmas

Joined: Apr 18, 2005
Posts: 24
I revise a little in your code here.

This is the sample of the variable that refer to the sam object.So,change one will effect one.It like pass by reference,but not.

Pass by reference in my understand is the Object that pass to argument.The argument variable will keep the copy of reference(same refer as the original variable).So, if you change the attribute of the object thatpass to method.It will effect to the original variable that keep this referenvce too.And I can explain like this code.

If I miss please comment me.

Thank you.
[ April 22, 2005: Message edited by: Sunyaluk Boonmas ]
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
please anybody justfiy the above code..and
explain pass by reference in more details...

Sunyaluk Boonmas

Joined: Apr 18, 2005
Posts: 24
This explanation very clear, I think.

And this flash playing is interesting.I think it is very good to show how it work.

This it main url.

Hope it useful for you.

However if I am wrong in any point please tell me.

I will exam next week.

Don't get me started about those stupid light bulbs.
subject: Dan exam doubt 19
Similar Threads
A question on Dan's GC Question
Doubt in GC
Garbage Collection II
Garbage Collection