File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&S CHAPTER 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&S CHAPTER 3" Watch "K&S CHAPTER 3" New topic
Author

K&S CHAPTER 3

akash azal
Ranch Hand

Joined: Jan 31, 2009
Posts: 70
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


We will keep things moving!!
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

What do you think the answer is ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
akash azal
Ranch Hand

Joined: Jan 31, 2009
Posts: 70
B c1 is eligible for GC
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

akash azal wrote:B c1 is eligible for GC


That would be correct
akash azal
Ranch Hand

Joined: Jan 31, 2009
Posts: 70
but in K&S answer is c
Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45
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



Regards
Sunny Mattas
SCJP5
akash azal
Ranch Hand

Joined: Jan 31, 2009
Posts: 70
thanks for clearing doubt
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
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?


SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
Byju Joy
Ranch Hand

Joined: Sep 06, 2005
Posts: 84
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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
 
Don't get me started about those stupid light bulbs.
 
subject: K&S CHAPTER 3