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!