• 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

How many object is eligible for garbage collection?

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from Devaka's diagnostic exam. I think it should be only one , but the correct answer is two object is eligible for garbage collection. I wonder where is the second object?

 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Morteza,

This question is asked many times in this forum. Please read this topic.

Devaka.

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, this is a tricky question if you are not thinking about operator precedence, and the fact that assignment has right associativity.

A d=new A().aob=new A();
is equivalent to
A d = ((new A().aob) = new A());
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the trick here is after the line:

the annonymous inner class object becomes eligible for GC.
 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!

And just to clear any doubt, there is not any "annonymous inner class" invloved. The pitfall of this question, like already is said, is the right associativity of assignment operator. That's all!
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Morteza Manavi-Parast wrote:Thank you!

And just to clear any doubt, there is not any "annonymous inner class" invloved. The pitfall of this question, like already is said, is the right associativity of assignment operator. That's all!


You are correct, Morteza.

I hope your studies are going well. I (and I'm sure many other people) have missed your insightful posts (the one about inner classes was a classic.)
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shin Kudo wrote:the trick here is after the line:

the annonymous inner class object becomes eligible for GC.



Where is the anonymous inner class in this?? There is just a temporary intermediate object with no reference which becomes eligible for GC (I've marked it in bold). I hope this doesn't goes over everyone's heads

A d=new A().aob=new A();
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

Shin Kudo wrote:the trick here is after the line:

the annonymous inner class object becomes eligible for GC.



Where is the anonymous inner class in this?? There is just a temporary intermediate object with no reference which becomes eligible for GC (I've marked it in bold). I hope this doesn't goes over everyone's heads

A d=new A().aob=new A();



You can say that again! There is nothing called anonymous inner class. Exactly, it's temporary object. And after all, there are two objects legal for GC. Right?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yen hoang wrote:there are two objects legal for GC. Right?



True
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yen hoang wrote:You can say that again! There is nothing called anonymous inner class.



Well actually I didn't see that people have already pointed it out that there is no anonymous inner class in this code, if that's what you mean. If you want to say that there is no anonymous inner class in this code, then it's fine. If you want to say that there are no anonymous inner classes in java, then I'm afraid you are wrong...
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

yen hoang wrote:You can say that again! There is nothing called anonymous inner class.



Well actually I didn't see that people have already pointed it out that there is no anonymous inner class in this code, if that's what you mean. If you want to say that there is no anonymous inner class in this code, then it's fine. If you want to say that there are no anonymous inner classes in java, then I'm afraid you are wrong...


Yes, I made mix up. I just want to say that in that code, there is not anonymous inner class. Thanks and regards!
 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ruben, I'm happy to hear that you found it useful!
And once again thank you for clearing my doubt on this question.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
first of all thanks ruben.clearing the doubt with out any complications.
from the above discussion my understanding is, when we call the gc() it will remove the
objects which are not reffered by any reference variable and the objects
having the null value.
am i right???
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NagarajGoud uppala wrote:
from the above discussion my understanding is, when we call the gc() it will remove the
objects which are not reffered by any reference variable and the objects
having the null value.
am i right???



Well it is not true. A call to gc() is just a request to the JVM to run the garbage collector. It is not sure if any objects will be garbage collected. This is why questions containing explicit call to gc() have been removed from SCJP 6.

The only thing that is sure about garbage collector is that it will run once before throwing an OutOfMemoryException. Other than that, you can't say when the garbage collector will run. In the exam, you'll only get questions asking how many objects are ELIGIBLE for garbage collection. No one will ask you how many objects are garbage collected as it cannot be determined...
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,
thanks for your awesome explanation
you mean even gc() run explicitly or implicitly we can't make sure that all eligible objects are removed??
am i right? or correct me
 
You get good luck from rubbing the belly of a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic