• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Page 259,Question 3rd from K&B

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Below question is from K&B chapter 3 ,page 259...my question is when c1.go mehtod is called c2 is initailised to null and c1 is also assigned null...so what happen with c3...in book answer is 2...can any one explain me how.
 
Sheriff
Posts: 6828
1298
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only c1 and c3 objects will be eligible for the GC. The object referenced by c2, will not be eligible.



Look at the above method. The c2 object is passed to this method. Before the cb=null; statement is executed, there are two references to the passed object. First one is the "CardBoard c2". And the second one is the method parameter "CardBoard cb". The statement "cb=null;" releases the reference of the method parameter, cb only. But the object is still having a reference to the c2 variable. Therefore, that object will not be eligible for GC.

Devaka.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Devaka Cooray wrote:Only c1 and c3 objects will be eligible for the GC. The object referenced by c2, will not be eligible.


I agree with most of what Devaka said, but there are no objects to which c3 references. The go method returns a null object,
so there are no objects eligible for the garbage collector. But there are two other objects eligible. Check this out:

How many objects did this line create? Two. It created a new Cardboard object (c1) and a new Short object (c1.story).
These are now eligible for the garbage collector.

cheers
Bob
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Wheeler wrote:

Devaka Cooray wrote:Only c1 and c3 objects will be eligible for the GC. The object referenced by c2, will not be eligible.


I agree with most of what Devaka said, but there are no objects to which c3 references. The go method returns a null object,
so there are no objects eligible for the garbage collector. But there are two other objects eligible. Check this out:

How many objects did this line create? Two. It created a new Cardboard object (c1) and a new Short object (c1.story).
These are now eligible for the garbage collector.

cheers
Bob



Bob, why will the Short object be created? It is not present in the Carboard constructor.
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Wheeler wrote:
How many objects did this line create? Two. It created a new Cardboard object (c1) and a new Short object (c1.story).
These are now eligible for the garbage collector.



Yes. Those are the two objects eligible. But nothing to do with the c3 reference.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Wheeler wrote:

Devaka Cooray wrote:Only c1 and c3 objects will be eligible for the GC. The object referenced by c2, will not be eligible.


I agree with most of what Devaka said, but there are no objects to which c3 references. The go method returns a null object,
so there are no objects eligible for the garbage collector. But there are two other objects eligible. Check this out:

How many objects did this line create? Two. It created a new Cardboard object (c1) and a new Short object (c1.story).
These are now eligible for the garbage collector.

cheers
Bob



in the specified question, only 2 objects are eligible for garbage collection, when c2 is passed into the c1.go(c2), a copy of the refrence is passed, later this copy is made to null but still original c2 object is being refered by a valid refrence so c2 will not be collected.About c3 as c3 is right now refering to a valid object later when we call c1.go(c2), the return of this method gives a cardboard object, you must have studied that when objects are returned from a method they are not eligible for GC.

If you agree with me just post a quote.....

Bhavuk
New Delhi
 
aabir sanyal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pondered on the question, and found that there are two objects created here (c1 and c2), whenever we call new. c3 doesn't create a new object.

Now according to Bob, Short is also creating an object. My question is how?? Because there is no constructor instantiating the Short object. Please explain a bit more.
 
Sheriff
Posts: 9687
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question has incorrect answer in the book as far as I remember. It says that two objects will be eligible for GC but only 1 object will be eligible for GC and that is c1. The Short will be taken from the constant pool so it will not be eligible for GC...
 
aabir sanyal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:This question has incorrect answer in the book as far as I remember. It says that two objects will be eligible for GC but only 1 object will be eligible for GC and that is c1. The Short will be taken from the constant pool so it will not be eligible for GC...



1.) Short also has a constant pool??

2.) K&B please come to rescue
 
Ankit Garg
Sheriff
Posts: 9687
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you've read K&B properly, then it is given that there is a pool of integer Wrapper objects in the range -128 to 127...
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aabir sanyal wrote:Pondered on the question, and found that there are two objects created here (c1 and c2), whenever we call new. c3 doesn't create a new object.
Now according to Bob, Short is also creating an object. My question is how?? Because there is no constructor instantiating the Short object. Please explain a bit more.


Doesn't matter if it is in the constructor or not. It is declared and instantiated as an instance variable. But the point where I was wrong, it didn't create a new object, it created a new String Pool member. If the short value would be e.g. 200, than two objects would eligible for gc; this way only one.

cheers
Bob
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

bhavuk soni wrote:
in the specified question, only 2 objects are eligible for garbage collection, when c2 is passed into the c1.go(c2), a copy of the refrence is passed, later this copy is made to null but still original c2 object is being refered by a valid refrence so c2 will not be collected.About c3 as c3 is right now refering to a valid object later when we call c1.go(c2), the return of this method gives a cardboard object, you must have studied that when objects are returned from a method they are not eligible for GC.


You are right; a copy of the reference is passed and called cb. This local reference is later set to null and returned. But c3 never referred to a valid object.
c3 gets the return of c1.go(c2), which is the local reference cb. And this cb is null resulting into an c3 referring to null as well. So no object eligible here.
Also this method invocation has no effect on the references c1 and c2 as you already said.

cheers
Bob
 
aabir sanyal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Wheeler wrote:

aabir sanyal wrote:Pondered on the question, and found that there are two objects created here (c1 and c2), whenever we call new. c3 doesn't create a new object.
Now according to Bob, Short is also creating an object. My question is how?? Because there is no constructor instantiating the Short object. Please explain a bit more.


Doesn't matter if it is in the constructor or not. It is declared and instantiated as an instance variable. But the point where I was wrong, it didn't create a new object, it created a new String Pool member. If the short value would be e.g. 200, than two objects would eligible for gc; this way only one.

cheers
Bob



What is the difference if Short is 5 or its 200??
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aabir sanyal wrote:
What is the difference if Short is 5 or its 200??


Just read the last two posts from Ankit

cheers
Bob
 
aabir sanyal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:If you've read K&B properly, then it is given that there is a pool of integer Wrapper objects in the range -128 to 127...



Please tell me the page number. I have the 1.5 book
 
Sneha Kapoor
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one tell me about "when objects are returned from a method they are not eligible for GC." so does this mean when C3 = CB = null ...C3 is not eligible for GC

and we are saying only one object elgible for GC
 
Ankit Garg
Sheriff
Posts: 9687
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Wheeler wrote:
Doesn't matter if it is in the constructor or not. It is declared and instantiated as an instance variable. But the point where I was wrong, it didn't create a new object, it created a new String Pool member. If the short value would be e.g. 200, than two objects would eligible for gc; this way only one.

cheers
Bob



Or Pool of Short objects
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sneha Kapoor wrote:Can any one tell me about "when objects are returned from a method they are not eligible for GC." so does this mean when C3 = CB = null ...C3 is not eligible for GC
and we are saying only one object elgible for GC


If an object is returned from a method and a reference variable is assigned to that return value like this

than that object is not eligible for GC, because there is still a reference var. pointing to that object (in a live thread).
BUT in our case the Null was returned (or CB which referred to the Null). So no object is eligible here at this point.
c3 itself is a reference variable, so it can't be eligible for GC in any case.

cheers
Bob
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:
Or Pool of Short objects


You got me.

cheers
Bob
 
Sneha Kapoor
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah i got it thanks
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What i understood is as follows

when the 1st line is executed

First 1 object is created in heap. This object contains reference variable 'story' of type 'Short'. Had it not be initialized(story), nothing would have happened. Since it is initialized to 200(or whatever), a new object 'Short' is created in heap, and its reference is stored in variable 'story'. So 'c1' has total of 2 objects in heap.

when next line is executed

same as above, 'c2' has 2 objects in heap.

when next line is executed

Before assigning any thing to 'c3', first 'go' method of c1 is executed with passing 'c2' to 'cb'. But immediately in next line, cb is made 'null' and the value of cb(which is null) is assigned to 'c3'. So 'c3' is now initialized to null. So nothing is created in heap by 'c3'.

now next line

2 objects created by 'c1' are toast.

remaining are the only 2 objects that are referred by 'c2'.
So as K&B said, only 2 objects(of c1) are eligible for GC.

Hope i am correct. Please clarify...


 
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to Abir sanyal:
See its the rule that when ever an object of particular class is created the instance variables are initialized. Same as the instance block. instance block runs after the constructors call to this or super. So no matter if it is inside the constructor or out side the constructor. and if you'll put the instance variable inside the constructor it will become local variable of the constructor and will not apply to other overloaded constructor. and when it'll not remain instance variable it will available to only those object which invoke the particular constructor having that local variable. Hope this will help.
Thanks & Regards
Jeetendra...!!
 
Sneha Kapoor
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Madhu that was wonderful way of expalaining
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Madhu Desai got it quite well, but the problem is, at the original exercise story has value 5. Short wrapper also has cached values, so I think the answer in the book is really incorrect.
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In K&B 6 book there is Short = 200;
With Short = 5 I think the answer would be different.
 
Madhu Desai
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lukas Smith wrote:In K&B 6 book there is Short = 200;
With Short = 5 I think the answer would be different.



I think it doesn't matter what value short has (in this example...). Because, short is a instance variable of that particular object, and every object that has been declared has to have its own copy. Which in turn means objects have to be physically created separately.

The only time, the concern of what the Wrapper object(Short and Integer only) is having like - is it within -128 and 127, is when you use equality operator like == to compare values between identical Wrappers. Where exactly will it store the values to save memory (if its between -128 & 127) is JVM's headache. We don't have to worry about that. Only thing to be concerned is, if the value is between -128 and +127, and you use == operator, it says both are same objects.

Remember, short has values between -32,768 to +32,767. You can store any value between it. And Wrappers are just primitives disguised as Objects, so that we can use them with objects. What remains in Wrappers are just plain primitives.

Hope my thoughts are correct...
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lukas Smith wrote:In K&B 6 book there is Short = 200;
With Short = 5 I think the answer would be different.



Yes... Also, this has been corrected in the Errata for earlier editions. So, this debate is kinda moot. Yes, the book is wrong, and it has been corrected in the Errata.

Henry
 
Henry Wong
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Madhu Desai wrote:
The only time, the concern of what the Wrapper object(Short and Integer only) is having like - is it within -128 and 127, is when you use equality operator like == to compare values between identical Wrappers. Where exactly will it store the values to save memory (if its between -128 & 127) is JVM's headache. We don't have to worry about that. Only thing to be concerned is, if the value is between -128 and +127, and you use == operator, it says both are same objects.

Hope my thoughts are correct...



But you also need to understand why this is true... The reason why -128 to 127 is a concern with the equality operator, is that autoboxing uses the valueof() method, which in turn uses a "cache" of objects.

Since values within this range is retrieved from a cache, they won't be GC'ed when they are no longer referenced, because the cache still has a reference to them.

Henry
 
Madhu Desai
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
But you also need to understand why this is true... The reason why -128 to 127 is a concern with the equality operator, is that autoboxing uses the valueof() method, which in turn uses a "cache" of objects.

Since values within this range is retrieved from a cache, they won't be GC'ed when they are no longer referenced, because the cache still has a reference to them.

Henry



Hmmm... I didn't knew that.

Thanks..
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic