hello debasmita,
for your firt question, yes, the Short reference var is created when you create an instance of CardBoard class.
concerning my prevois reply, I'll explain what I understood from the book (before reading the book, I had no idea about this particularity )
look at the following example:
case 1:
Integer i1 = 1000; //created with autoboxing, not with "new"
Integer i2 = 1000; // the same here
if you
test i1==i2 you will get "TRUE" (the same object)
case 2:
Integer i1 = 10;
Integer i2 = 10;
if you test i1 == i2 you will get "FALSE" (different objects)
case 3:
Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
if you test i1 == i2 you will get "FALSE" (different objects), because they were NOT created with AUTOBOXING
Explanation:
In the first case, the JVM creates two DIFFERENT objects with the value 1000.
In the second case, the JVM, to save some memory, create ONLY ONE object in the heap, that is referenced by both i1 and i2, because i1 and i2 were created with autoboxing, and 10 < 127
In the third place, the JVM creates two different objects, even though 10<127, because they were created with "new", and not with autoboxing.
This was what I understood from the theory, and I have tested it, so it is correct.
Now, back to the test form the book:
normally, when you create c1 , the Short object is created for it, and when you create c2, another Short object is created for it.
when you say c1 = null, then the Short object which belongs to c1 will also be lost, right ?
So, there are 2 objects eligible for GC. The only problem is that with wrappers things don't work like this, except for the cases where the values are > 127.
BUT, if the Short objects are created with autboxing (Short story = 5, like in the test question), and the value is < 127, then when you create c1, there will be one Short object created , and when you create c2, there will NOT be created another Short object, but it will reference to the previously created one. So, both c1 and c2 will SHARE THE SAME Short object. In this case, if you write c1 = null, the Short object won't be lost, because it will still be referenced by c2.
Conclusion: in order to be correct the response in the test (2 eligible objects for GC), they changed the value of the Short object from 5 to 200, because if they hadn't change the value, the correct answer whould have been: only one eligible object for GC, more explcit only c1 whould have been eligible for GC.
I hope you understand what I ment, and if anybody thinks I'm wrong, please tell me.