0

Why is it possible to return a memory address in Java while it is not allowed to return an address in C?

I understand that C does not allow to return an address since the memory location used by a function in C will be freed up after it is done executing. However, Java allows the return of a reference to the an Object

INVALID code in C returning the address to an array of character (String)

String function () {
    char String2 [3] = "Hi";
    return String2;
}

Valid code in Java returning the reference to String

public String method () {
    String test = "Hi";
    return test;
}

is there perhaps a difference in the way the memory is managed in Java as compared to C? I would like to know why is possible to return an address in Java.

P.S. I understand that Java has an automatic garbage collector for the objects in the heap. But what I don't understand is that why is it able to still be able to return the reference to the objects. Thank you!

Janjan
  • 85
  • 7
  • 1
    Maybe you want to rephrase your question? I don;t think you're looking for a yes or no answer. – Sourav Ghosh Jul 27 '20 at 15:24
  • @AndyTurner, how is it different, I would like to understand why is it possible to return an address in Java – Janjan Jul 27 '20 at 15:24
  • @Janjan try searching for "java vs c memory management" in your preferred search engine. – Andy Turner Jul 27 '20 at 15:26
  • @AndyTurner, I understand that Java has an automatic garbage collector for the objects in the heap. Does that mean that since the objects are not automatically garbage collected, the method will be allowed to return the reference to the object? – Janjan Jul 27 '20 at 15:29
  • C and Java are different languages with different rules. – Andrew Henle Jul 27 '20 at 15:36
  • @AndrewHenle, hi may I please know why Java allows the user to return an address? – Janjan Jul 27 '20 at 15:37
  • 1
    Language is not really an issue here... the real issue is heap vs stack allocations. If java allowed for manual stack allocation you wouldn't be allowed to return a reference to such object either.You can safely return the address of malloc-ed object in C as long as it is properly freed later on. – suff Jul 27 '20 at 15:47
  • @suff, why is it that address can be return in heap allocations as opposed to stacks? – Janjan Jul 27 '20 at 15:49
  • @Janjan because when you return from method/function the stack frame is no longer there, thus you would be returning an address to an expired object. When you allocate something on the heap it is alive up until the point where you free it (which is, as was mentioned in other comments, something that is in java done by garbage collector). – suff Jul 27 '20 at 15:59
  • @suff I understand that Java automatically "garbage collects" if there are no variables referring to the object, but since in the above example there will always be a reference to the object thus it will not be automatically garbage collected making it possible to return the address to the object. Is my understanding correct? – Janjan Jul 27 '20 at 16:06
  • 1
    @Janjan in the above example the reference is being returned, so yes it surely does exist at that point. – suff Jul 27 '20 at 17:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218757/discussion-between-janjan-and-suff). – Janjan Jul 28 '20 at 14:32

0 Answers0