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
  • 21,442
  • 29
  • 112
  • 288
Joey.Z
  • 121
  • 5
  • 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
  • There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs. – gnat Aug 22 '13 at 11:49
  • @gbjbaanb I've been used Java for years. I am a student, though I didn't have a deep insight into this language, I can use it quit proficiently. It will help me in finding jobs in Android development or web development. – Joey.Z Aug 22 '13 at 11:52
  • 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
  • What resources that aren't managed by garbage collector in Java? – Uooo Aug 22 '13 at 12:14
  • possible duplicate of [Did the developers of Java consciously abandon RAII?](http://programmers.stackexchange.com/questions/118295/did-the-developers-of-java-consciously-abandon-raii) See also: [Why use try … finally without a catch clause?](http://programmers.stackexchange.com/questions/131397/why-use-try-finally-without-a-catch-clause) – gnat Aug 22 '13 at 12:15
  • 1
    @Uooo any [closeable](http://docs.oracle.com/javase/7/docs/api/java/io/Closeable.html) 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 06:08

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
  • 146,727
  • 38
  • 279
  • 479
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.

Emilio Garavaglia
  • 4,289
  • 1
  • 22
  • 23
  • 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:3.1.2.4 Storage duration of objects](http://port70.net/~nsz/c/c89/c89-draft.html#3.1.2.4) may not be the OO definition you have in mind, but it's definitely called an object. – Useless Aug 22 '13 at 16:02
  • Yes, I can even call yourself an object, if you like. Are you human or a compiler? – Emilio Garavaglia Aug 23 '13 at 13:26
  • C has objects, the syntax is just slightly different: `customer.addOrder(orderId)` vs. `addOrder(&customer, orderId)`. I don't know why you'd use pure C for a business system but that's the first example that came to mind. Look at the VB script documentation for Excel 2000 to see a real messy mix between those syntaxes. – Mark K Cowan Jun 21 '14 at 11:19
  • Of course, you can always call by a natural language standpoint whatever *type* a *class* and whatever *variable* an *object*. I can rewrite whatever programming language grammar by using that terminology (touring equivalence exist for whatever finite state machine, an compilers are as such as well). The fact that C specs talk about "objects" doesn't make it "oriented to them". It has them, but doesn't offer any primitive to handle them (there is no polymorphic behavior and resource management). But it seems to me I'm just touching some religion dogma. – Emilio Garavaglia Jun 21 '14 at 19:06