Differences between Java and C++

Started by
23 comments, last by rip-off 15 years, 3 months ago
I consider myself a fairly experienced C++ programmer, with a good knowledge of the fundamentals and the techniques that should (theoretically) carry over pretty well to another language. Now I am being required to use Java for a computer science class I'm taking, and having never played with the language before, I thought I'd ask here for pitfalls to watch out for. I'm not looking for the things that are called out in the numerous "Java for C++ Developers" tutorials on the net, just stumbling blocks that have been encountered by real-world people. Thanks in advance, everybody. P.S. I also have some C# knowledge. I've heard they're similar?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Advertisement
Coding in java is nothing more than writing atrocious C++ code.

1. It encourages the use of 'new' without any considerations. Obviously, no 'delete's are necessary because of the garbage collection, but in C++, using heap memory is something you would typically want to avoid when you can use the stack.

2. There is no 'const'. This is one of the greatest features of C++.

3. Java templates are basically a poor hack of the C++ version.

4. All objects are automagically treated as references.

5. No operator overloading.


As far as pitfalls go, just be sure to bring a nose plug. When coding in java, you may find a hideous stench rising from your keyboard.
Watch out for multiple inheritance hierarchies that use inner classes or interfaces.......
Also keep in mind that JAVA treats boolean values differently from integer values(a very common mistake i used to make)...something like while(20)...; is a n invalid statement in JAVA but pretty natural if youre working on C++....
Also some class hierarchies may seem a bit illogical to you but keep in mind that JAVA stores classes separately....id advise you to google up anonymous inner classes and adapter classes .......also keep in mind that in JAVA objects are passed by reference to functions and not by value........
Quote:Original post by EmrldDrgn
I'm not looking for the things that are called out in the numerous "Java for C++ Developers" tutorials on the net, just stumbling blocks that have been encountered by real-world people.


The biggest difference that I've ever encountered between Java and C++ programming is the IDE. With C++ I can use Visual Studio and Visual Assist X. Visual Studio is an amazing IDE with a powerful debugger and while it certainly has its flaws, no other language boasts a better IDE.

With Java, you have NetBeans, which admittedly is reminiscence of Visual Studio, but I never cared for their setup. Then there is Eclipse, which is often toted as one of the main IDEs you should know for cross language development, but like NetBeans, quite a few things just seem naturally "backwards" there. That view of course, could be from having used the way Visual Studio organized and architectured its projects. There is also JCreator, which I personally loved because it was very simple, lean, and had some nice syntax highlighting features, auto-complete, documentation integration, etc...

It's been about 2 years since I've had to write Java code though, so those views above were from a few year of Java development. I'm sure there are more IDEs today and those mentioned have been upgraded quite a bit, but I can't remember how great the debugging systems were. I know I had to use a lot of console.out.printlns though haha!

Anyways, that was and certainly would be one of the biggest issues I'd say you can see going from any Windows C++ development to another language. You get so used to having certain features that when they are no longer there, you feel your productivity decrease.
One thing to look out for: No structs.

As in if you need a copy of a collection of data (which will usually be a class) you need to explicitely call clone on it.
This has bitten me more than once, when i had a collection of objects. I took one of them, modified it for local uses, but that also changed the object inside the collection.

C# has this very useful distinction, all objects behave like references, while structs are really copied on an assignment.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Most of what people said can be boiled down to this:

Everything is a primitive or an object. Objects are always references.

Remember that and most of the pitfalls go away.
Quote:Original post by WhatsUnderThere
Coding in java is nothing more than writing atrocious C++ code.

1. It encourages the use of 'new' without any considerations. Obviously, no 'delete's are necessary because of the garbage collection, but in C++, using heap memory is something you would typically want to avoid when you can use the stack.

2. There is no 'const'. This is one of the greatest features of C++.

3. Java templates are basically a poor hack of the C++ version.

4. All objects are automagically treated as references.

5. No operator overloading.


As far as pitfalls go, just be sure to bring a nose plug. When coding in java, you may find a hideous stench rising from your keyboard.


this sounds like someone who has no meaningful experience in java. to rebut:

1. there is the final keyword, same thing

2. their templates are pretty much the same, just called generics instead.

3. they're not references in the C++ sense, they're just called references in java.

4. yeah, i wish java let you overload operators, but thats hardly a huge reason to hate it.

and by the way, there is no reason why using the heap is better than using the stack, and in C++ you should try avoiding dynamic memory(using the heap) whenever you can anyways, since it's probobly the most expensive operation you can do. And using new with no consideration? that is because it doesn't have the same implications as it does in C++.


all this being said, I am a C++ programmer, and I do prefer using C++. I know java because I'm in school
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Quote:Original post by WhatsUnderThere
1. It encourages the use of 'new' without any considerations. Obviously, no 'delete's are necessary because of the garbage collection, but in C++, using heap memory is something you would typically want to avoid when you can use the stack.
Bear in mind, however, that heap allocation in Java is faster than heap allocation in C++ (in fact, it can get quite close to stack allocation speed) precisely because it's garbage collected: on a compacting garbage collector, allocating memory is done simply by incrementing a pointer (whereas in C++, heap allocation requires traversing the heap looking for some free space).

Quote:3. Java templates are basically a poor hack of the C++ version.
Java does not have templates. Java has generics, which are a fairly different concept. C++ templates, in addition to providing correct yet obfuscated metaprogramming functionalities, also make a poor attempt at emulating generics (sadly, without all the type-system friendliness generics have).
Quote:Original post by godsenddeath
1. there is the final keyword, same thing
Nope. In C++, a const variable prevents you from altering the object represented by the variable (this forbids calling non-const methods, among other things), whereas in Java, a final variable prevents you from assigning to the variable more than once (this allows calling any kind of method afterwards). There is no way in Java to specify "You Shall Not Modify This Object", unlike in C++.

Quote:2. their templates are pretty much the same, just called generics instead.
Generics and templates are different beasts (mostly because templates were a first initial attempt at implementing generics, but computer science wasn't advanced enough yet to handle the complex type algebras this implies). A generic will know whether an array-of-base is an array-of-derived or vice-versa, whereas a template has no way to do so. Arguably, Java generics are still quite primitive.
Quote:Original post by godsenddeath
this sounds like someone who has no meaningful experience in java.

...

Quote:1. there is the final keyword, same thing

No. See internet.

Quote:2. their templates are pretty much the same, just called generics instead.

No. See internet.

Quote:4. yeah, i wish java let you overload operators, but thats hardly a huge reason to hate it.

No. If Java had templates, operator overloading would be a great benefit. You could even write a EBNF implementation in Java, like it exists for C++ (boost spirit).

Quote:all this being said, I am a C++ programmer, and I do prefer using C++. I know java because I'm in school

Java? School? Meaningful experience? Insulting people? And then, just claims that are provably (and widespread known as) false?

edit: Was your meaningful experience in Java really back in school, or did you learn it in your now first year at the university? Just curious.

This topic is closed to new replies.

Advertisement