2

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 resources 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 resources 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?

gnat
  • 20,543
  • 29
  • 115
  • 306
Joey.Z
  • 121

2 Answers2

4

Java 7 has try-with-resources to handle non-memory resources. You can implement java.lang.AutoCloseable to use it with your own objects. RAII wasn't an original design intent for destructors, it was "discovered" later and only came into vogue in the late 90s, early 2000s, after Java was created. That's why Java 1-6 didn't have any RAII-like construct.

Karl Bielefeldt
  • 148,830
1

Actually the question make some sense, because all the three languages somewhat tightened by a same overall history, from VonNeuman machines to C as a "high level assembler", to the need to industrialize code re-usability and the introduction of the idea of "object".

In C this is just "function and data and opaque types". After a formalization of object oriented methodologies, C++ added to C the idea of function overload and runtime-based polymorphism, with the drawback to grant object life across functions, and the consequence of object lifetime management.

Java comes soon after, embracing the OOP paradigm and being designed around it and around the idea of "platform independence" (thus creating the n+1 platform: the java machine). By constraining all object to be dynamic, it solved the problem of C++ pointer, introducing garbage collection and unifying pointer and object into a common syntax (the . notation).

Similarly C++ evolved towards a more generalized language, not tightening into OOP, but introducing templates and lambdas, opening that way towards other programming paradigm (like generic programming, and functional programming) and resource management methodologies (like RAII, reference counting and the like) embedding the memory management problems into library and purposed defined classes.

For what nowadays concern, Java gorws where resource control is not "the issue" but where having a large multiplatfom OOP codebase is straightforward. C++ is more appreciated where resource control and high optimization is a must (a compiler can hack down to the asembler instruction set of a machine, Java has to remain at a java machine level, thus breaking the optimization process in two distinct pieces) and where algorithm and hardware are required to better cooperate.


To answer to the final question, in absence of deterministic destruction, deterministic resource management have to be handled explicitly with finalization methods (like "on_final" or similars) eventually exposed by interfaces (like "finalizable") to be eventualli chained in collection, to be explcitly called where appropriate.