File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But passing a primitive is different from passing a reference...

am i right?

regards,
rajani.
 
David Ng
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Anuradha Shanmugam
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Sanyev Babu
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou soo much all of you for your replies..

Regards,
rajani.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic