Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3. class Dozens {
4. int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};
5. }
6.
7. public class Eggs {
8. public static void main(String[] args) {
9. Dozens [] da = new Dozens[3];
10. da[0] = new Dozens();
11. Dozens d = new Dozens();
12. da[1] = d;
13. d = null;
14. da[1]= null;
15. // do stuff
16.
17. }
18.
19. }
20.

PLEASE READ CAREFULLY...THANKS

Question: How many objects are created within the "main() method",
and how many of those are eligible for garbage collection when line
14 is reached?¿?

Answer: five objects created and two objects eligible for garbage
collection. (REMEMBER "WITHIN THE MAIN METHOD").

The above answer would be correct if:
Dozens [] da refers to an object of type "Dozens array".

And if:
each newly created Dozen object has its own int array Object, meaning:
da[0] = new Dozens(); are two objects!!

1. Dozens Object.
2. int[] array Object

And, does this have anything to do with the "HAS-A" theory?¿?

I will be greatfull if anyone can clearify my doubts.

 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags to write codes. Thanks
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whatever you are saying is true. The explaination is perfect. Yes It is because of the Has-A relationship between Dozen and the int array.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha Daga wrote:Please use code tags to write codes. Thanks

Thanks...I understand you and would appreciate better if after correcting
me "on quote tags", you end it up clearifying my doubts okay?¿? Thanks.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well I did that.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha Daga wrote:well I did that.

Thanks body...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please => SearchFirst <= and => UseCodeTags <=. Discussed recently!
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ikpefua, there are a few guidelines that you should keep in mind while posting on javaranch. Using Code Tags is one of them. When you use code tags around your code, the code is easier to read. Also please mention the source when you post a mock exam question...
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your information/advise, as you can see Im completely new in this game, such info would help me construct better posts in future. Thanks once again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic