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.