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?

  • 1
    I'd just take java off your CV, as your skills with it don't seem to be helping you. – gbjbaanb Aug 22 '13 at 11:47
  • 1
    @gnat I just describe some background in the front, the main question I am asking is at the end. – Joey.Z Aug 22 '13 at 11:54
  • 1
    @Uooo any closeable mostly the IO classes – ratchet freak Aug 22 '13 at 12:21
  • 2
    @zoujyjs you're proficient in Java but you don't know what a finalizer is? Or a try..finally block? You're right that the GC is a good tool for memory management, and a poor one for object lifetimes, but the design of java is such that they used the GC for object lifetimes too. I'd try to do more NDK work for Android, and asp.net mvc for web - the latter seems to be much more in demand today, and the former is good for writing significantly better android apps. – gbjbaanb Aug 22 '13 at 14:27
  • 1
    I'd sum the difference thusly: C++ is an advanced multi-paradigm language allowing especially functional programming and metaprogramming while maintaining excellent performance at the cost of being somewhat difficult to master. Java is intentionally dumbed down to make it easy to learn. While garbage collector helps productivity, it can get rather verbose due to lack of other features. If Java is not frustrating to you, you don't know C++! – Jan Hudec Aug 23 '13 at 6:08

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.


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.

  • 2
    How was object life across functions created by C++? I can clearly remember using dynamic allocation in C. – Useless Aug 22 '13 at 13:05
  • In C you don't allocate "objects": malloc allocates memory you use to store data. In C++ new allocates constructed object on the dynamic memory normally allocated via C malloc. The Idea of an Object to construct and destroy comes with C++, not C. – Emilio Garavaglia Aug 22 '13 at 15:32
  • Nope, the C standard describes an object as a region of data storage in the execution environment, the contents of which can represent values. – Useless Aug 22 '13 at 15:36
  • Today C does, not the C (1989) I was raferring to. – Emilio Garavaglia Aug 22 '13 at 15:58
  • 1
    C89 draft: Storage duration of objects may not be the OO definition you have in mind, but it's definitely called an object. – Useless Aug 22 '13 at 16:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.