Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Do Exceptions thrown are eligible for Garbage Collection?

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a bit confusing question for me (Whizlabs SCJP 5.0 - Final Exam - Question 63):

How many objects will be eligible for garbage collection after the execution of line 11 in the following code snippet?

1. public Integer getNumber(String num)
2. {
3. int i = -1;
4. Integer number = null;
5.
6. try{
7. i = Integer.parseInt(num);
8. }
9. catch(NumberFormatException ignore){}
10. number = new Integer(i);
11. return number;
12. }


Possible answers: (choose 1)

A.- 1
B.- 0
C.- 2
D.- 3

-----------------------------------------------------
By the time I tried to solved this question. I chose A (1 object) I did know primitive local variables are created in the stack, so they are not eligible for Garbage Collection, but I looked for the exception thrown.

Are exceptions thrown eligible for Garbage Collection?. They are Objects and they use constructors so they live in the heap.

I would apreciate any help.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Javier,

I did know primitive local variables are created in the stack, so they are not eligible for Garbage Collection


Only objects are eligible for the garbage collection(GC).

In the problem above(after line 8)


Objects created
at line 7.
at line 6.
Since the method returns the reference to the object created at line 7 so object created at line 7 will not be GCed.
The NumberFormatException created at line 6 will be GCed as the reference to the object will be destroyed after line 6.
So only one object will be GCed and that is the object created at line 6.
 
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Kumar Singh:
Javier,

Only objects are eligible for the garbage collection(GC).

In the problem above(after line 8)


Objects created
at line 7.
at line 6.
Since the method returns the reference to the object created at line 7 so object created at line 7 will not be GCed.
The NumberFormatException created at line 6 will be GCed as the reference to the object will be destroyed after line 6.
So only one object will be GCed and that is the object created at line 6.




That depends on if a NumberFormatException is thrown or not, which we cannot say as we do not know what the String num is. If we assume it is a vaild number (which i think is the best interpretation) then 0 objects are eligible for GC arter this method. If it get thrown by parseInt then the answer is 1 as it is eligible for GC after the catch block completes.
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Sanjeev Kumar Singh & Mark Smyth. I appreciate it.
 
He baked a muffin that stole my car! And this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic