Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

argument returning

Thread view: 
josh - 03 Oct 2006 08:45 GMT
Hi, if I have a method like this:

public static int[][] pass()
{
    int a[][] = {{1,2},{3,4}};

    return a;
}

and than

int z[][] = pass();

when I call this method it returns the value of 'a' that is
a pointer (reference) to the matrix created. So in the caller
z has that value.
But I have a doubt, when the method return the variable 'a' is garbaged
and
thus also its value so how is possible that z has that value?
May be because the method returns to 'z' a copy of the 'a' value  like
any variable value?

In the c/c++ when I return a matrix or a pointer in the function that
variable must be set
as static otherwise it loose its value...

Is that wrong?

Thanks
Manish Pandit - 03 Oct 2006 08:53 GMT
Hi,

What you get is a copy of a. The method has 'a' declared as a local
variable, and it is method-scoped. Variable z[][] will be {{1,2},{3.4}}
till you explicitly change it.

-cheers,
Manish
josh - 03 Oct 2006 09:54 GMT
Manish Pandit ha scritto:

> Hi,
>
> What you get is a copy of a.

that it is a reference that point to that matrix values ...

>The method has 'a' declared as a local
> variable, and it is method-scoped.

so when it returns a is garbaged  ...

> Variable z[][] will be {{1,2},{3.4}}
> till you explicitly change it.

because it now point to the reference copy pointed by 'a' ...

Correct interpretation?
Matt Humphrey - 03 Oct 2006 13:04 GMT
> Manish Pandit ha scritto:
>
[quoted text clipped - 3 lines]
>
> that it is a reference that point to that matrix values ...

Yes, you get a copy of the reference to object.  There is still only one
object.

>>The method has 'a' declared as a local
>> variable, and it is method-scoped.
>
> so when it returns a is garbaged  ...

"a" is not garbage collected.  It is a variable (a name) and it goes out of
scope at the end of the method.  This gives rise to the possibility that
whatever it was pointing to may become inaccessible (and hence eligible for
garbage collection), but the matrix will not be eligible for collection
because there is still a live reference to it.  The garbage collector will
reclaim eligible objects at its discretion later.

>> Variable z[][] will be {{1,2},{3.4}}
>> till you explicitly change it.
>
> because it now point to the reference copy pointed by 'a' ...

z points to the same (identity) object that a pointed to.

> Correct interpretation?

Better.

Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
josh - 03 Oct 2006 14:02 GMT
Matt Humphrey ha scritto:

> "a" is not garbage collected.  It is a variable (a name) and it goes out of
> scope at the end of the method.  This gives rise to the possibility that
> whatever it was pointing to may become inaccessible (and hence eligible for
> garbage collection), but the matrix will not be eligible for collection
> because there is still a live reference to it.  The garbage collector will
> reclaim eligible objects at its discretion later.

so only objects when have not more reference to them will be garbaged
and not the variables in which there  are their references.
Matt Humphrey - 03 Oct 2006 15:59 GMT
> Matt Humphrey ha scritto:
>
[quoted text clipped - 10 lines]
> so only objects when have not more reference to them will be garbaged
> and not the variables in which there  are their references.

I think you've got it--it's a matter of distinguishing between the variable,
the reference and the object.  Variables hold references to objects and as
long as the variable is in scope, the object can be reached.  When the
variable goes out of scope the reference it was holding disappears but the
object still stays. The object will only be eligible for garbage collection
when it can no longer be reached from any point in the program.

I find pictures helpful for this-- http://www.iviz.com/memory%20pointer.gif 
A variable going out of scope is the same as having its box (on the diagram)
disappear.  It doesn't change the objects at all, but if there are no longer
any arrows to objects, that object is eligible for garbage collection.

Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
josh - 04 Oct 2006 08:16 GMT
Matt Humphrey ha scritto:

> I think you've got it--it's a matter of distinguishing between the variable,
> the reference and the object.  Variables hold references to objects and as
> long as the variable is in scope, the object can be reached.  When the
> variable goes out of scope the reference it was holding disappears but the
> object still stays. The object will only be eligible for garbage collection
> when it can no longer be reached from any point in the program.

Thanks the answer has been very clear


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.