1

This question has been on my mind for a while. I used C++ for some web development I was doing, and also some hobby programming. The only reason I got into it a little was because of the web development thing, as I just used plain C before. All in all, I didn't use C++ too extensively.

Recently, I've been doing more extensive work with Java. Lots of people say that C++ and Java are completely different, and that doing thing the 'Java way' isn't a good idea for programming in C++, suggesting that they are two completely different worlds. They certainly feel different, and function differently. But more specifically, what do people mean when they say that?

Are they talking about garbage collection? Or something else?

Thanks

  • They are talking about *everything*. It's the whole mindset that differs between C++ and Java. – Xeo Jan 07 '12 at 15:19
  • 1
    Probably best served by asking on programmers.stackexchange.com. – Perception Jan 07 '12 at 15:20
  • 1
    @Xeo: Haha. Comments like yours are exactly what I was referring to. :) I get the different mindset, but more specifically, what are the style differences? –  Jan 07 '12 at 15:20
  • 2
    if you can't absolutely understand it, its C++ – Felice Pollano Jan 07 '12 at 15:20
  • @Hassan: Java shoehorns everything into classes. Everything has to derive from `Object`. C++ let's you do as you please, you can have free function, aswell as member functions. Same with the inheritance. If you need a Java-like hierarchy, you need to build it yourself, because C++ doesn't need it. We have templates that are far more powerful than anything Java has to offer (no bashing, this is a fact). They allow duck-typing so that we don't need a uniform interface to operate between objects. – Xeo Jan 07 '12 at 15:24
  • 1
    On the other hand, Java is a more *productive* language, just because so much is done for you already. If you however want to disable certain parts? Well, tough luck. Now, if I continue any further, I think I'll derail into bashing... – Xeo Jan 07 '12 at 15:25
  • @Xeo: Thanks for the insight. Like I said, I only used C++ for a small web application, and probably didn't utilize 10% of its capabilities. –  Jan 07 '12 at 15:27

3 Answers3

2

Main difference is due to the fact that with Java you are running a program on a virtual environment (Java Virtual Machine) that you do not know, and that you are not allowed to access deeply.

This obviously generats a lot of differences, regarding level of programming and efficiency.

You can check this interesting link I found: http://en.wikipedia.org/wiki/Java_performance

And this SO question: C++ performance vs. Java/C#

Speaking in general terms C++ allows you to have more low level control of your programming.

e.g Java garbage collector manages memory without you knowing what it does:

  1. you don't know when it starts
  2. you don't know how it works
  3. . . .

instead in C++ if you want a sort of garbage collector module you must manage memory allocations by yourself.

Community
  • 1
  • 1
Matteo
  • 7,924
  • 24
  • 84
  • 129
  • Yes, but C++ can be used for user applications. Even then, people would suggest that they are entirely different. –  Jan 07 '12 at 15:22
  • @Hassan working low-level doesn't mean you can't program user applications... – Matteo Jan 07 '12 at 15:26
  • "instead in C++ you must manage memory allocations." -- Must you? I don't do any memory management in C++. – Benjamin Lindley Jan 07 '12 at 15:26
  • @BenjaminLindley If you wish to implement a sort of garbage collector module you must implement it by your self – Matteo Jan 07 '12 at 15:29
  • 1
    @Matteo: If I want garbage collection, I go to Java or C#. In C++, we have smart pointers. – Xeo Jan 07 '12 at 15:31
  • @Matteo: Okay, but why would I want that when I can just create objects with automatic storage that will destroy themselves at the end of the scope? – Benjamin Lindley Jan 07 '12 at 15:35
  • @BenjaminLindley mine was just a fast and direct example of difference in low vs high level way of programming, I didn't say that it should be smart to implement it. I agree that probably I could have found a better example, but it was the first that came up in my mind since Hassan cited it.. – Matteo Jan 07 '12 at 15:38
  • @BenjaminLindley Not all data structures can be freed purely based on scope. Sometimes you need to use a heap, rather than a stack. – Peter Lawrey Jan 07 '12 at 15:41
  • @Matteo but you know how your malloc-free mechanism works in C++? – Thorbjørn Ravn Andersen Jan 07 '12 at 15:42
  • @PeterLawrey: That's why we have copy constructors and return values. And for objects that cannot or should not be copied, we have smart pointers, which themselves are still stack based. – Benjamin Lindley Jan 07 '12 at 15:45
  • @ThorbjørnRavnAndersen you're talking about new() and delete()? – Matteo Jan 07 '12 at 15:48
  • @BenjaminLindley While stack based it is still managed memory because you don't have to explicitly free up the memory. – Peter Lawrey Jan 07 '12 at 16:05
  • @PeterLawrey: Exactly right, that was my original point. That in C++, just because we don't have built-in garbage collection, doesn't mean we have to manually manage memory, ever. ~90% of the time, we just use automatic storage, where in Java you would use dynamic storage. When we need dynamic storage, we have smart pointers, or container classes. – Benjamin Lindley Jan 07 '12 at 16:14
  • @BenjaminLindley C++ still has the option of using a garbage collector, but using automatic storage is more the C++ way. Java's allocation is efficient up to the point you need to perform a collection. What Java lacks is the option to use objects on the stack or automatic collection. Personally, I think allocating on the stack is a real improvement which the JVM has only limited optimisations to work around. (It can eliminate the object allocation, but rarely does) IMHO, The automatic allocation feels more like a hack or an after thought to me. – Peter Lawrey Jan 07 '12 at 16:25
  • 1
    @PeterLawrey: We are in agreement, I think. – Benjamin Lindley Jan 07 '12 at 16:29
2

This article, as well as this one can help you with this matter. As to actually working with languages, I find C++ easier to work with, and giving me much more freedom over my data than Java does.

Bugster
  • 1,552
  • 8
  • 34
  • 56
0

There so many specific differences between a high level language like Java and a low level language like C++ that it would be best to leave it to Wikipedia to list them out: http://en.wikipedia.org/wiki/Comparison_of_Java_and_C%2B%2B

I strongly recommend building out a project in both of them to get a better understanding. The similarities really stop at the syntax.

Dan Hardiker
  • 3,013
  • 16
  • 19