| Author |
K&B ch-3 Ques-2
|
pradeepta chopra
Ranch Hand
Joined: Jul 05, 2008
Posts: 137
|
|
2. Given: When // doStuff is reached, how many objects are eligible for GC? A. 0 B. 1 C. 2 D. Compilation fails. E. It is not possible to know. F. An exception is thrown at runtime. Answer: C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible. A, B, D, E, and F are incorrect based on the above. (Objective 7.4) why is only object c1 eligible for gc? isnt c2 being nulled in go method which in turn returns a null to be assigned to c3?? So should'nt c2 and c3 be eligible for GC??
|
 |
pradeepta chopra
Ranch Hand
Joined: Jul 05, 2008
Posts: 137
|
|
isnt c2 and c3 be eligible for GC
it should be isnt c2 and c1 be eligible for GC?? since c3 is just a reference variable
|
 |
ramesh maredu
Ranch Hand
Joined: Mar 15, 2008
Posts: 210
|
|
when control reaches go method there are two reference variables for second object(CardBoard c2 = new CardBoard()), those are one is c2 and other is cb, here cb is made null but c2 is still pointing to the object. And correct answer is only one object is eligable for GC as story is referencing the object taken from constant pool. [ October 30, 2008: Message edited by: ramesh maredu ]
|
SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
Originally posted by pradeepta chopra: it should be isnt c2 and c1 be eligible for GC?? since c3 is just a reference variable
First of all, objects gets GC when there is no valid reference to it.. now when you passes 'c2' as an parameter to go method , and holds it in 'cb' (parametrized local reference of type CardBoard ) , and then on next line you null it , then that doesn't mean you are assigning 'null' object to 'c2' object.. Its there in memory with 'c2' as a referenced , scoped in 'main' method.. AND 'c3' is just an reference and pointing to null object ( you can considered it as a reference varibale , uninitialized..) Hope this clarifies a bit..
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
pradeepta chopra
Ranch Hand
Joined: Jul 05, 2008
Posts: 137
|
|
yes i get the point Thanks all
|
 |
Shiks Sethi
Greenhorn
Joined: Nov 01, 2008
Posts: 10
|
|
So, we still have 2 objects available for GC- correct! 1. Object referenced by c1 reference variable----which can no more be referenced as c1=null. 2. Short Object which c1 object has reference to----which can no more be referenced as c1 cannot be referenced. Sorry, but I am not very clear on Ramesh's post of Short object from the constant pool.Can anyone clarify that? Thanks
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16680
|
|
Sorry, but I am not very clear on Ramesh's post of Short object from the constant pool.Can anyone clarify that?
Basically, the Short object is assigned with autoboxing. Autoboxing uses the valueOf() method to assign the object. The valueOf() method, uses a cache for certain values -- of which 5 is one of those values. So, the Short object is not GC'ed, because it is still held in the cache.... And BTW, you should take a look at the Errata page for the K&B book. The short value is changed to a value that is not cached. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Shiks Sethi
Greenhorn
Joined: Nov 01, 2008
Posts: 10
|
|
Thanks for pointing out the Errata. So from what you said- 1. If code has Short story=5; Answer= 1 obj available for GC. 2. If code has Short story=200 Answer= 2 obj available for GC. Is that rite? How do i know which values would be cached and which not? Like you said 5 is one of those values....
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
Originally posted by Shiks Sethi: T How do i know which values would be cached and which not? Like you said 5 is one of those values....
Here is the range Character from \u0000 to \u007f Short and Integer from -128 to 127
|
 |
Ganeshkumar cheekati
Ranch Hand
Joined: Oct 13, 2008
Posts: 362
|
|
1 public class FindRanges 2 { 3 public static void main(String[] args) 4 { 5 System.out.println("Integer range:"); 6 System.out.println(" min: " + Integer.MIN_VALUE); 7 System.out.println(" max: " + Integer.MAX_VALUE); 8 System.out.println("Double range:"); 9 System.out.println(" min: " + Double.MIN_VALUE); 10 System.out.println(" max: " + Double.MAX_VALUE); 11 System.out.println("Long range:"); 12 System.out.println(" min: " + Long.MIN_VALUE); 13 System.out.println(" max: " + Long.MAX_VALUE); 14 System.out.println("Short range:"); 15 System.out.println(" min: " + Short.MIN_VALUE); 16 System.out.println(" max: " + Short.MAX_VALUE); 17 System.out.println("Byte range:"); 18 System.out.println(" min: " + Byte.MIN_VALUE); 19 System.out.println(" max: " + Byte.MAX_VALUE); 20 System.out.println("Float range:"); 21 System.out.println(" min: " + Float.MIN_VALUE); 22 System.out.println(" max: " + Float.MAX_VALUE); 23 } 24 } output is: Integer range: min: -2147483648 max: 2147483647 Double range: min: 4.9E-324 max: 1.7976931348623157E308 Long range: min: -9223372036854775808 max: 9223372036854775807 Short range: min: -32768 max: 32767 Byte range: min: -128 max: 127 Float range: min: 1.4E-45 max: 3.4028235E38. these are the actual ranges of wrapper classes. from K&B book(page no:236): In order to save memory, two instances of the following wrapper objects will always be == when their primitive values are the same: Boolean Byte Character from \u0000 to \u007f (7f is 127 in decimal) Short and Integer from -128 to 127. According to me: if two Short (or) Integer objects are created with in the range of -128 to 127 then the result is true by using == operator. so,if we mention Short value as 200.i think it will not effect the answer which is two objects are eligible for garbage collection. correct me if i am wrong.
|
SCJP5 and SCWCD1.5
Think Twice Act Wise...
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
Originally posted by Ganeshkumar cheekati: so,if we mention Short value as 200.i think it will not effect the answer which is two objects are eligible for garbage collection. correct me if i am wrong.
Right, any Short OR Integer value greater than range -128 to 127 is constructed in to an object on heap ..
|
 |
 |
|
|
subject: K&B ch-3 Ques-2
|
|
|