Java Forum / First Aid / April 2004
newbie question on returning an object
Ryan - 28 Apr 2004 22:58 GMT 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 - 28 Apr 2004 23:12 GMT > 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 - 28 Apr 2004 23:55 GMT "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 - 29 Apr 2004 00:01 GMT "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
Ryan - 29 Apr 2004 03:46 GMT Thanks, I see. It's the garbage collection...
Roedy Green - 29 Apr 2004 00:46 GMT >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.
Yang Xiao - 29 Apr 2004 13:36 GMT > 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 - 29 Apr 2004 13:44 GMT >> Hello, I am a C programmer, but just start learning Java. >> When returning an object, if only the address is copied from the local [quoted text clipped - 5 lines] > 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
 Signature Kind regards, Christophe Vanfleteren
Yang Xiao - 29 Apr 2004 18:18 GMT > >> Hello, I am a C programmer, but just start learning Java. > >> When returning an object, if only the address is copied from the local [quoted text clipped - 19 lines] > > won't be erased. > > Yang Can you elaborate on "reachable reference", how does that differ from reference counting? many thanks,
Yang
Christophe Vanfleteren - 29 Apr 2004 18:36 GMT > 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();
}
 Signature Kind regards, Christophe Vanfleteren
VisionSet - 29 Apr 2004 13:46 GMT > > Hello, I am a C programmer, but just start learning Java. > > When returning an object, if only the address is copied from the local [quoted text clipped - 7 lines] > 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 - 29 Apr 2004 15:53 GMT > 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 - 29 Apr 2004 16:02 GMT 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?
> 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 - 29 Apr 2004 16:07 GMT > 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.
 Signature Kind regards, Christophe Vanfleteren
Free MagazinesGet 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 ...
|
|
|