Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

K&S CHAPTER 3

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2. Given:
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 // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 3
D.compilation fails
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think the answer is ?
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B c1 is eligible for GC
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

akash azal wrote:B c1 is eligible for GC



That would be correct
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but in K&S answer is c
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but in K&S answer is c


hi
There is a mistake with this question in K&S.
Book says second object garbage collected is


But this will not garbage collected because value of short is under 127 which means it is in cache memory.
If value is changed to


then two objects will be garbage collected.

Regards
Sunny Mattas
SCJP5


 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for clearing doubt
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But this will not garbage collected because value of short is under 127 which means it is in cache memory.



What is the logic behind this?

I am completely unaware of the logic used here. Can somebody explain?
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is the logic behind this?



I believe it's 10% of logic + 20% of heuristic + 30% of assumption + 40% of confusion behind this.

Statistically speaking, according Pareto's principle, 80% of numeric variables are using only 20% of possible values. 80% of Short variables take values in the range of 0-127 only. So they are 'cached' or 'shared'.

Later when economy improves, and memory cards becomes cheaper this decision could be revised to cache all Short values.
 
author
Posts: 23919
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

Aakash Goel wrote:
What is the logic behind this?

I am completely unaware of the logic used here. Can somebody explain?



The "cache" being referred to here, is the cached in the Short class -- basically, the class will pre-instantiate a certain number of Short objects in a range... Later, when the valueOf() method is called, and the value is within the range, it will return the object from the cache, instead of creating a new one. And since autoboxing uses the valueOf() method, it uses the cache.

In relation to this question, the short is within the range, hence, it can't be GCed, because the object returned is still in the cache. In the errata, the short value is changed so that it is not in the cache.

Henry
 
Henry Wong
author
Posts: 23919
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

Byju Joy wrote:
Later when economy improves, and memory cards becomes cheaper this decision could be revised to cache all Short values.



Interestingly, this can be done at any time, without the need to change the specification. The specification defines what range must be cached. It doesn't say what should be done with values outside of the range. Hence, a version of the JVM can be released, which caches everything, and it will still be compliant with the Java specification.

Henry
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic