• 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

Fellows, please come in and check this question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following code, how many objects will be eligible for garbage collection on the line with the comment //here

A 1; B 2; C 3; D 4;

And after code line 7 "findOut(x);" the orignial Integer variable x(line 6) is set to NULL or still referring to the object Integer(10)?



(edited:no need for the bangs in the topic title)
[ February 08, 2007: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 12, no objects will be eligible for garbage collection. But when the function returns, two objects will be eligible.

After line 7, x will still points to the object Integer.
 
Jiayun Chen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Thanks a lot.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Could somebody pls explain in more details? I am not able to get it. Isn't it that when the value is between -128 and 127, the wrapper object will be created in the object pool? Which two objects are eligible for GC?
 
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

Originally posted by Kiran Gavate:

Could somebody pls explain in more details? I am not able to get it. Isn't it that when the value is between -128 and 127, the wrapper object will be created in the object pool? Which two objects are eligible for GC?



When you call the "new" operator, A new object will be instantiated. The JVM doesn't know about or use an object pool for this. This object pool that you are referring to, is related to the Integer class... specifically, the valueOf() method checks to see if the resulting Integer is within the -128 and 127 range. And if it is, it will return an object that it had constructed eariler.

This valueOf() method is what is used internally by autoboxing. No autoboxing is used in this example, nor is the valueOf() method used.

Henry
[ February 09, 2007: Message edited by: Henry Wong ]
 
Kiran Gavate
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick explanation, but sorry I still do not understand which two objects will be eligible for GC. Could you please explain?
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two Integer objects are created in the BettyAck()-constructor (lines 6 and 8). Both Integer objects are eligible for garbage collection as soon as the constructor has finished "his work". Whether this happens on line 12 or line 13 is a bit of cutting hairs... So one can say there are no eligible objects in line 12, and another guy would answer that there are two objects eligible for garbage collection at this point (because after executing line 11 the constructor is done as long as there is no additional code following).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic