For Programmers: Free Programming Magazines  


Home > Archive > Java Help > April 2004 > newbie question on returning an object









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author newbie question on returning an object
Ryan

2004-04-28, 6:59 pm

Hello, I am a C programmer, but just start learning Java.
When returning an object, if only the address is copied from the local
object to the external object, then once the callee function exits, the
local object will be erased; what will this do to the external object that
receives the address? Will it be erased too? Thanks.


VisionSet

2004-04-28, 6:59 pm


"Ryan" <Ryan@aol.com> wrote in message
news:c6p9ef$es959$2@ID-154755.news.uni-berlin.de...
> Hello, I am a C programmer, but just start learning Java.
> When returning an object, if only the address is copied from the local
> object to the external object, then once the callee function exits, the
> local object will be erased; what will this do to the external object that
> receives the address? Will it be erased too? Thanks.
>


No there is only one object, the method has a copy of the reference only.
The reference is lost the object remains. In Java references to the object
are passed, and they are passed by value.

--
Mike W


Bjorn Abelli

2004-04-28, 8:01 pm


"Ryan" wrote...

> When returning an object, if only the address is
> copied from the local object to the external
> object,


There's a difference between pointers in C and references in Java, but I
don't think we need to that deep right here...

> then once the callee function exits, the
> local object will be erased;


No, it won't be erased...

> what will this do to the external object
> that receives the address?


....as the "external" variable still will hold a *reference* to the object.

Objects are not "value" types such as structs in C, but a completely
different kind of animal.

Example, lets say we call a method:

Object o = myMethod();

And then we have the method:

Integer myMethod()
{
// 1. x now holds a reference to an object.
// The variable x is created on the stack,
// referencing to the object on the heap

Object x = new Object();

// 2. y now holds a reference to an object.
// The variable y is created on the stack,
// referencing to another object on the heap

Object y = new Object();

// 3. the method returns the *reference* to
// the first object

return x;

// 4. As the method returns, the local
// *variables* within the method are
// eliminated from the stack, but...
}

....both objects still exist on the heap.

As the result of the call, o is referencing the first object of the two, but
there's no variable referencing the second object.

Now the second object is *eligable* for garbage collection, as there are
*no* reference to it, but the first one is still referenced.

I don't know if this made it any clearer, but I hope so...

// Bjorn A


Bjorn Abelli

2004-04-28, 8:01 pm


"Bjorn Abelli" wrote...

> Integer myMethod()


Sorry for the typo. It should of course be an Object instead of Integer:

Object myMethod()
{
Object x = new Object();
Object y = new Object();
return x;
}

// Bjorn A


Roedy Green

2004-04-28, 8:54 pm

On Wed, 28 Apr 2004 17:58:36 -0400, "Ryan" <Ryan@aol.com> wrote or
quoted :

>Hello, I am a C programmer, but just start learning Java.
>When returning an object, if only the address is copied from the local
>object to the external object, then once the callee function exits, the
>local object will be erased; what will this do to the external object that
>receives the address? Will it be erased too? Thanks.


Nope. The callee's local reference disappears, but the object remains
and the pointer to it returned on the stack. The object won't be
garbage collected because the caller will soon be pointing to it with
one if its local variables as soon as it saves the returned value.

It is safe in the interim because any object pointed to by the stack
won't be gc'd either.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Ryan

2004-04-29, 1:14 am

Thanks, I see. It's the garbage collection...

"Bjorn Abelli" <DoNotSpam.bjorn_abelli@hotmail.com> wrote in message
news:c6pcoj$elt2b$1@ID-73292.news.uni-berlin.de...


Yang Xiao

2004-04-29, 2:23 pm

"Ryan" <Ryan@aol.com> wrote in message news:<c6p9ef$es959$2@ID-154755.news.uni-berlin.de>...
> Hello, I am a C programmer, but just start learning Java.
> When returning an object, if only the address is copied from the local
> object to the external object, then once the callee function exits, the
> local object will be erased; what will this do to the external object that
> receives the address? Will it be erased too? Thanks.


isn't gc works automatically in Java and objects are only gc'ed if
there is no more reference to it? So gc bascially counts the
references to each object and acts when the number reaches 0. So in
your senario, there's still one more reference to it at least, so it
won't be erased.
Yang
Christophe Vanfleteren

2004-04-29, 2:23 pm

Yang Xiao wrote:

> "Ryan" <Ryan@aol.com> wrote in message
> news:<c6p9ef$es959$2@ID-154755.news.uni-berlin.de>...
>
> isn't gc works automatically in Java and objects are only gc'ed if
> there is no more reference to it? So gc bascially counts the
> references to each object and acts when the number reaches 0.


No the GC in a JVM doesn't use reference counting.

Simply put, Objects can only get GC'd when there are no more reachable
references to it. So objects that reference each other circularly can still
get GC'd, which wouldn't be possible if a simple refcounting scheme was
used.

So if A references B, and B references A, they can still get GC'd, if
nothing else has references to A or B.

> So in your senario, there's still one more reference to it at least, so it
> won't be erased.
> Yang



--
Kind regards,
Christophe Vanfleteren
VisionSet

2004-04-29, 2:24 pm


"Yang Xiao" <yang2002_99@yahoo.com> wrote in message
news:55c6e631.0404290436.5e15e880@posting.google.com...
> "Ryan" <Ryan@aol.com> wrote in message

news:<c6p9ef$es959$2@ID-154755.news.uni-berlin.de>...
that[color=darkred]
>
> isn't gc works automatically in Java and objects are only gc'ed if
> there is no more reference to it? So gc bascially counts the
> references to each object and acts when the number reaches 0. So in
> your senario, there's still one more reference to it at least, so it
> won't be erased.


AFAIK it isn't quite as simple as that, it depends on the JVM. It depends
how reachable a reference is. For instance singletons are eligible for gc
in some JVMs.

--
Mike W


Mark Haase

2004-04-29, 2:24 pm

In article <c6p9ef$es959$2@ID-154755.news.uni-berlin.de>,
"Ryan" <Ryan@aol.com> wrote:

> Hello, I am a C programmer, but just start learning Java.
> When returning an object, if only the address is copied from the local
> object to the external object, then once the callee function exits, the
> local object will be erased; what will this do to the external object that
> receives the address? Will it be erased too? Thanks.


Depends what you mean. If you're returning using the keyword, ie.

return myObj;

then as long as you assign that result to a variable, the object will
not get destroyed.

If you have Object myFunc(int p, int p2, ..), then

myFunc(1,2,...);// will result in the return being lost, whereas
Object o = myFunc(1,2,..); // will result in it not being GC'ed

If, on the other hand , you're talking about returning a value through a
parameter, as is typical with C, then no you can't do that. Ie. in C you
can say

void copyVal(int *p) {
*p = 5;
}

int a;
a = 1;
copyVal(&a);
// a now has 5 stored in it

There's no way to do this in Java.

--
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
Ryan

2004-04-29, 2:24 pm

Or, in other words, Objects in Java were all created in the heap, with
new(), so that they don't get automatically erased when existing a stack
frame?

"Ryan" <Ryan@aol.com> wrote in message
news:c6p9ef$es959$2@ID-154755.news.uni-berlin.de...
> Hello, I am a C programmer, but just start learning Java.
> When returning an object, if only the address is copied from the local
> object to the external object, then once the callee function exits, the
> local object will be erased; what will this do to the external object that
> receives the address? Will it be erased too? Thanks.



Christophe Vanfleteren

2004-04-29, 2:24 pm

Ryan wrote:

> Or, in other words, Objects in Java were all created in the heap, with
> new(), so that they don't get automatically erased when existing a stack
> frame?


Exactly.

--
Kind regards,
Christophe Vanfleteren
Yang Xiao

2004-04-29, 2:24 pm

Christophe Vanfleteren <c.v4nfl3t3r3n@pandora.be> wrote in message news:<RM6kc.90506$8Z1.5666952@phobos.telenet-ops.be>...[color=darkred]
> Yang Xiao wrote:
>
>
> No the GC in a JVM doesn't use reference counting.
>
> Simply put, Objects can only get GC'd when there are no more reachable
> references to it. So objects that reference each other circularly can still
> get GC'd, which wouldn't be possible if a simple refcounting scheme was
> used.
>
> So if A references B, and B references A, they can still get GC'd, if
> nothing else has references to A or B.
>

Can you elaborate on "reachable reference", how does that differ from
reference counting?
many thanks,

Yang
Christophe Vanfleteren

2004-04-29, 2:24 pm

Yang Xiao wrote:

> Can you elaborate on "reachable reference", how does that differ from
> reference counting?
> many thanks,
>
> Yang


Consider the following:

public void someMethod() {

A a = new A();
B b = new B();
a.setB(b);
b.setA(a);
//now there are 2 refs to a and 2 refs to a
//the a and b refs, and the refs to b in a and to a in b.
return;
}
//now a and b can get GC'd although
//a still has ref to b and b still has ref to a
//since neither a or b can get reached outside of the method.

If you used refcounting, a and b wouldn't be eligible for GC, since they
still have references pointing to them (from b and a respectively). The
local a and b references will be destroyed when returning from the method.

But since you, as the programmer, no longer have any "direct" references to
a or b, those objects are no longer reachable, so they become eligible for
GC, even though they are still being referred to.

If we had returned either a or b from the method, the objects would still be
reachable, since we could get to a or b using the reference in b or a:

public A someMethod() {

A a = new A();
B b = new B();
a.setB(b);
b.setA(a);
//now there are 2 refs to A and 2 refs to B
return a;
}

public void otherMethod() {

A a = someMethod();
//neither a or b can get GC'd, since we now can still reach a, and also b
using a.getB();

}

--
Kind regards,
Christophe Vanfleteren
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com