Without C++-like destructors, how do we return resources that aren't
managed by garbage collector in Java?
I'm in a job hunting. And in my CV I placed a skill list like:
Skills: C/C++/Java/...
The most common question I got is: "hem, since you are familiar with both
C++ and Java, can you tell some similarities or difference between the two
languages."
And I just don't know how to answer, what I said is basically some
language level details like they have some different keywords like
Interface,abstract and so on. I want to see some comparison in high level
like the difference in generics, the garbage collector and so on.
At least I want to go deep into one side, that is the resource management.
Java has no lifetime for an object, this is managed by the garbage
collector, and in C++ you need to carefully manage your resource
especially for the heap. In C++ we can greatly reduce the memory leak by
introducing RAII, using object to manage the heap memory, and so is for
the other resouces like connection,lock and so on. I am not sure what to
do in Java, because the garbage collector can only be a nice tools for the
management of heap memory( AFAIK ).
Question: How can we manage other resouces in an situation that we do not
have a destructor to do all these automatically? Do we need to manually
guarantee that the resources be returned in a right place. And how?
No comments:
Post a Comment