• 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
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Return values of methods

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a method has a return type, either a primitive or an Object type or any other user-defined type, what happens to the return value if it is not used.
For eg., if I have a hashtable MyHash, I would use the put() method to fill it up. The put() has a return type of Object. However, I don't need the return type so I ignore it. Does the returned Object occupy any memory space...or is it garbage collected if it is not referenced....what happens to this lost value??
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can be assured that it will be available for garbage collection as soon as the method exits (i.e. returns). Just think of the return value as an assignment to a variable, as such
MyObject = getMyObject(); // this method returns a MyObject
The return value now has a reference and is not available for gc. Therefore, if you do this
getMyObject();
then the MyObject instantiated in the method is not referenced and, therefore, is available for gc.
Sean
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sean,
what is gc as in

The return value now has a reference and is not available for gc.


Cheers,
Kate
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kate
gc = garbage collection
 
Matty Mathew
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks folks, u've been a help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic