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

GC ??

 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After which line will the object created at line XXX be eligible for garbage collection?

Ans
//2
//3
//4
Never
Can't say
ans is Never ... I say Can't say ..as you do not know when the caller(of this method) will release the object
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have to just focus on the code you see, and not other code that you know is there, but not part of the question.
If you just look at the method, it's clear that the object being returned is not eligible for garbage collection ever, in this method.

Rob
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
In my exam I found a question similar to yours. But the "D" answer spells "Never for this method". Maybe who was make your question forgot write "for this method". But remember in the exam select the more closer to the truth or the truth.
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wish tha I won't get ambiguous method ..
 
Junilu Lacar
Bartender
Pie
Posts: 6532
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravish,
Take all practice questions and their answers with a grain of salt. Don't worry about ambiguity in the real exam. The folks at Sun have gone to great lengths to make sure that the questions in the real exam are not ambiguous. As mentioned in RHE:
There are no trick questions on the exam, but every question requires careful thought. The wording of the questions is highly precise; the exam has been reviewed not just by Java experts, but also by language experts whose task was to eliminate any possible ambiguity

(emphasis by jl)
 
Paul Salerno
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because:
c = b; //2
the memory location for object assigned to b, hence, when b is assigned to something else, the object that b once held is now in c, correct? I know we went thru one similar to this
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
that's correct but b still references the object until it is assigned some other object or null.
HIH
 
Paul Salerno
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin,
Which is in this case correct?
b = a = null
Fair to say that the other object is GC'd:
Object c,d = new Object()
c = b
following this assignment?
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Salerno:
Valentin,
Which is in this case correct?
b = a = null
Fair to say that the other object is GC'd:
Object c,d = new Object()
c = b
following this assignment?

The new object referenced by variable d will be eligible for garbage collection when the method ends. You aren't setting d to null anywhere in this method, so in the method, the object referenced by d is never eligible for gc. However, since d is a local variable, it ceases to exist after the method ends, so at that time, the object referenced by d is eligible for gc, since there are now no references to it.
Rob
 
Paul Salerno
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last word on this GC. I'm really have great notes thanks to you all!
so in this case:
Object c = new Object()
c = b
obj referenced by c would be eligible for GC.
however here:
Object c,d = new Object()
c = b
d also references the same object which isnt being set to null anywhere. since d is a local var it will cease to exist following the completion of the method, then that object ref by d is eligible for GC.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware though that in
Object c,d = new Object();//line 1
c=b; //line 2
only d is initialized on line 1 and c is not... c is initialized on the line 2
HIH
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Junilu Lacar:
Ravish,
Take all practice questions and their answers with a grain of salt. Don't worry about ambiguity in the real exam. The folks at Sun have gone to great lengths to make sure that the questions in the real exam are not ambiguous. As mentioned in RHE:

(emphasis by jl)

Hope to catch those precise wordings.....
 
Paul Salerno
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin,
I didnt know that, I thought they both were.
-Paul
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Salerno:
Valentin,
I didnt know that, I thought they both were.
-Paul

Try this:

What is the output ?
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wont compile !
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roy Ben Ami:
wont compile !

I believe this is Val's point. It shows clearly that the assignment is only happening to variable j.

Rob
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic