I am studying for a Java Certification Exam. I approached this problem in a book written by "Sierra-Bates", where I need to solve multiple choice question. When the //do stuff is reached, how many objects are eligible for GC?
The code is given below, and I also use print statement inside the constructor to count the number of Objects created.
Obviously, to me it looks like 2 Objects are created, and 1 Object is eligible for Garbage Collection, since c1 (instance variable) is pointed to null.
However, the answer (given in the book) is 2 Objects are eligible for Garbage Collection. The explanation written is, "Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible"
My Question is, why the "Short story" variable is counted as an Object, and eligible for Garbage Collection?
Short story = 200;
System.out.println("CardBoard Object: " + toString());
public static void main (String  args)
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
c1 = null;
I would appreciate if you could explain the "explanation" given in the book :)