• 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

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question1 :


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

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


Answer: g (none of the above)..


Question 2:

Which of the following is a true statement?

a. The object created a line 1 is eligible for garbage collection after line 2.
b. A NullPointerException is generated at line 3.
c. The program compiles, runs and prints X.
d. The program fails to compile.
e. None of the above

Answer: c.

Can anyone explain why a1 is not elligible for garbage collection in Question1?

And why "i" is not elligible in Question 2?

Thanks in advance..

rajani.

(code tags added - please learn how to do this)
[ May 19, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your first question: remember that the reference a1 in the method m1 is a copy of the reference a1 created in the main method. Setting a1 to null in m1 does not set the a1 in main to null. Compare with passing a primitive value such as an int.


The same analysis holds for your second question.
[ May 19, 2004: Message edited by: Barry Gaunt ]
 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But passing a primitive is different from passing a reference...

am i right?

regards,
rajani.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reference itself is passed just like a primitive. You can modifies the field refered to by the reference but any changes made to the reference stays local.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajani,

Why dont you have a look at this wonderful flash example given by Corey @ http://www.geocities.com/mcglonec1978/javacert/javacert.html#param_passing ?

Regards,|
Anu
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In both the cases you are passing a reference to the methord m1. Inside method m1 there is a local reference a1 and i respectively. They are local to the methord m1 and does not have any relation with the instance reference. There reference get created inside the method m1 and loses its scope once the method finishes execution.

So if you make the local reference as null then only the local reference will not point to the required object. The instance reference continues to refer to the object.

The case is different if you try to change the value of the object. since the instance reference and local reference are pointing to the same object modification is possible to the object with both the reference.

Hope it is clear.

Sanyev
 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou soo much all of you for your replies..

Regards,
rajani.
 
today's feeble attempt to support the empire
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic