This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Return values of methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Return values of methods" Watch "Return values of methods" New topic
Author

Return values of methods

Matty Mathew
Greenhorn

Joined: Jun 07, 2001
Posts: 7
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??
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
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
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Sean,
what is gc as in
The return value now has a reference and is not available for gc.

Cheers,
Kate
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Kate
gc = garbage collection


Dave
Matty Mathew
Greenhorn

Joined: Jun 07, 2001
Posts: 7
Thanks folks, u've been a help.
 
GeeCON Prague 2014
 
subject: Return values of methods