• 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

K&B ch-3 Ques-2

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i get the point
Thanks all
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
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

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
 
Shiks Sethi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic