• 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

Dan exam doubt 19

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
m1(a1);
System.gc();
}}

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 ?
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :



Regards
Balaji Pattabhiraman
[ April 22, 2005: Message edited by: Bajji Pat ]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[])
{
b=a;
b=b.append("xyz");
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
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya
please anybody justfiy the above code..and
explain pass by reference in more details...


thanx
 
Sunyaluk Boonmas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This explanation very clear, I think.

http://www.javaranch.com/campfire/StoryPassBy.jsp

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


http://www.geocities.com/mcglonec1978/javacert/paramPassing_highres.html

This it main url.
http://www.geocities.com/mcglonec1978/javacert/javacert.html#param_passing

Hope it useful for you.

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

I will exam next week.

cheer...
 
reply
    Bookmark Topic Watch Topic
  • New Topic