• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DOUBT from K&B

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i have a doubt regarding one question in K&B in page number 259.

here is the question:

class CardBoard{
Short story = 5;
CardBoard go(CardBoard cb){
cb = null;
return cb;
}
public static void main(String args[]){
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
//do stuff
}
}

When //do stuff is reached,how many objects are eligible for GC?

a. 0
b. 1
c. 2
d. Compilation fails
e. not possible to know
f. Exception thrown at runtime

i read the expalination but couldnt get it.
Can anyone help me in understanding the above?
thanks in advance!!!


 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
---------------------------------------------
Short story = 5;
--------------------------------------------
in the above question story is also one object.
[ May 08, 2007: Message edited by: anil kumar ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Two objects c1 and c2 are being created on lines 1 & 2. At line 3, c3 is set to null therefore there is no new object there.

In the method go() which was passed c2 reference as the method argument is being set to null, but this will not affect the object c2 back in the calling (main) method. Therefore, after the call to go(), c2 still holds a valid reference . Since go() returns null, c3 will be null.

At line 4, object c1 is set to null but c2 still holds a valid object reference. Therefore c1 and a Short wrapper object that it holds, both will be eligible.
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi krishnan,
thanks for clarifying the doubt.
where is the Short wrapper class coming into picture here?
i thought it was just given to confuse us.
do you mean that when the constructor of CardBoard is called,

Short story = 5;
the object of value 5 is created?
please clarify.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, CardBoard 'has-a' Short object. You need to count that as well.
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi krishnan,
can i conclude the following from your explaination:

a wrapper class object(if its an instance variable) is created when the class to which it belongs is instantiated.

please correct me if i am wrong.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

Look for the errata, because for this question the Short variable is not 5, it is 200. At begining I wonderred what's the difference ?
Well, in the K/B book, it sais:
"In order to save the memory, two instances of the wrappers objects, CREATED THROUGH AUTOBOXING, will always be equal when their primitives are the same"
Those wrappers that the rule applys to are: Boolean, Byte, Character, Short and Integer (from -128 to 127)
So, in the case that the Short var = 5 (Autoboxing), the value is < 127, so there will be only one Short obj created in the heap, so even if the c1 is null and the reference to the Short var is gone, the Short var is still references through c2.
So:
- if the Short var = 5 (5<127), there is only one (1) obj eligible for GC, that is c1
- if the Short var = 200 (5>127), there are 2 objects eligible for GC, because for c1 has its own Short object, and c2 has its own Short object.Only in this case the explanation in the book is true.
I hope I understood correctly , if not please tell me so we don't learn wrong things.

Thanks
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi camelia,
what i wanted to know was:

a wrapper class object(if its an instance variable) is created when the class to which it belongs is instantiated.

for eg:in the prevoius question
class CardBoard{Short story = 5;CardBoard go(CardBoard cb){cb = null;return cb;}public static void main(String args[]){CardBoard c1 = new CardBoard();
}

now i have got another doubt with your explaination.
if the Short var = 5 (5<127), there is only one (1) obj eligible for GC, that is c1
- if the Short var = 200 (5>127), there are 2 objects eligible for GC, because for c1 has its own Short object, and c2 has its own Short object.Only in this case the explanation in the book is true.

how is the range coming into picture and if its coming then why its specifically considered with respect to byte value range(i suppose)when the object creatd is of a short.

i am totally confused now.

 
camelia codarcea
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
camelia codarcea
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To undestand it better test if c1.story == c2.story for both cases (story = 5, and story = 200)
You will se that when story = 5, (c1.story == c2.story) is true, but when story = 200, the (c1.story == c2.story) is false.

I hope now the things are more clear for you.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Camelia wrote:


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)



Actually case 1: will return FALSE and case2: will return TRUE.
For numbers < 128, it returns TRUE.
 
camelia codarcea
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, that is right, I wanted to explain as clear as possible the situation, but apparently I made mistakes.

So, case 1: the "==" returns FALSE
case 2: the "==" returns TRUE
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
thanks!
i am now able to understand the concept
 
reply
    Bookmark Topic Watch Topic
  • New Topic