C++ versus Java

Pages: 123
hi, hoping this is not out of topic... i program in c++ and i'd like to improve it, but i'd be interested in java too... I'm sure many here have experience on both... i hear there'e lots of common things, but i guess java is still considered THE language..at least judging by its popularity...
Personally i found the move to C++ quite difficult as i was coming from non object-oriented languages or specific softwares (vba excel, matlab...).
I'm not after extremely advanced tasks, such as games etc.. just want to eventually be able to develop decently stable and reliable systems.
I guess starting from a c++ base would facilitate a move to java, what do you think?
thanks
Just forget everything you know about pointers and you're good to go.
Yea, I agree with chris. But one point I'd like to make : pointers are one of the BIGGEST strengths of C++. And you said you're interested in game dev, etc. So, hardware interaction plays a very vital role here where C++ probably wins(as it facilitates direct memory interaction using pointers).
Last edited on
Be ready to type a painful amount in Java.
"Just forget everything you know about pointers and you're good to go. "..

i wish it was that simple... :) , but anyway, sounds interesting/encouraging and anyway i'll certainly give it a try ..

i agree pointers are great stuff (although it took me a while to get familiar with them); no, not interested in games and such.. more interested in systems for the financial markets


ardengo wrote:
more interested in systems for the financial markets
Does that mean you're interested in devising softwares for the stock markets? Then trust me, you'll use a great deal of C++

Just as a side note, do you know that even NASA'a Curiosity rover has many parts programmed in C++?
Last edited on
closed account (NUj6URfi)
I am not sure what would be better for financial market systems but i prefer java due to the integrated graphics department, lack of pointers and finally automatic effective memory management.

The illusionist mirage wrote:

Just as a side note, do you know that even NASA'a Curiosity rover has many parts programmed in C++?

No I didn't.
Last edited on
yes, this seems to be the case.. the projects/programmers guys in my previuos company told me the treasury/currency mangement system was done in c++... i seem to see in many financial job positngs that c# is also quite used, but if i decide to learn a new language i would definitely go for java
closed account (N36fSL3A)
Please don't make one of these topics. It's just going to make a 13 page long thread where no one gets anywhere.
There are pros/cons to both, however I prefer C++ for it's usefulness and power.

Java:

Pros- Runs on ANY system (no porting!), probably learn it faster, and you might find it easier to write.

Cons- Can't be used to run high-cpu calculations (way too slow for that, aka no graphics engines with it, no refrencing hashes too much, etc...), can be vulnerable to modifications/hacks and is more prone to this than a binary executable, and there is no low-level programming functionality; all memory is automatically handled (so if you want to make your program more memory efficient, forget it).

C++:

Pros- Low level and high level programming, fast, not easy to hack a binary executable, powerful.

cons- does not run on any OS unless ported, I've been told it takes longer to write than java

**********************************************************

Upon my observations of human behavior, I notice that people tend to do what's most easy, and not necessarily what is most correct. I prefer C++ over java even if it "takes longer" to write, because at least then I know what my program is doing... but that's my preference.

Also, this is a topic that is hotly debated.
Last edited on
closed account (N36fSL3A)
I don't understand how C++ takes longer to write if Java has such long variable names.
thanks all for the replies... all very insightful.. yes, i see it can be a very subjective matter
Lumpkin: C++ takes longer to write, because it takes a thousand lines to do somthing Java has a pre-made object for.

Example: I wrote a class to contain a vector and display a part of it, while allowing to scroll up/down. I bet java has a function for that already written... took me 500 or 800 lines, but it works like a charm and is super fast, and, best of all, doesn't do anything other than what I need it to.
Last edited on
closed account (N36fSL3A)
I think you're over-exaggerating.
@IWishIKnew
The pros and cons aren't really as clear-cut as you seem to think.

[Java] Runs on ANY system (no porting!)
[C++] does not run on any OS unless ported

Java doesn't run on "any system", much less with "no porting". It runs on any system the Java interpreter is ported to (although that is quite an extensive number of systems). The C++ language is every bit as portable as Java -- the original Java compiler and virtual machine were implemented in C/C++. In theory, C++ can be ported to pretty much any architecture because it makes so few assumptions about the system. The portability of Java refers to code that's written in it, not the language itself. It refers to the fact that, once compiled, your code will run on any supported platform: Java code gets compiled to an intermediate form, called bytecode, which the JVM then converts to native code and executes. This means that you only have to create one binary, and then that binary will run on any JVM, hence the phrase "write once, run anywhere". C++, on the other hand, has no intermediate form; it gets compiled straight to native code which means you have to create separate binaries, although those binaries will typically run much faster (at the very least, they will load faster due to the lack of a need to convert from bytecode to native code). Also, the C++ standard library is much less extensive than Java's, which means that to do certain things you have to resort to platform-dependent code. Java also has well-defined integer sizes (int is always 32-bits) which C++ still lacks (std::uintnn_t is optional and might not be defined on some platforms). However, Java code isn't guaranteed to be portable just because it's written in Java. Not every platform has a JVM, though the important ones do, and there are still platform-dependent things that have to be taken into account, like endianness. Also, the original implementations of the Java GUI library had some differing behaviour between different platforms. So, to recap, the languages themselves are no more or less portable, and a given piece of C++ code can be as portable as an equivalent piece of Java code; Java's advantage where portability is concerned is that you only need to manage one codebase and distribute one file, and that running on a virtual machine allows it to make certain guarantees that C++ can't make without affecting portability.

[you can] probably learn [Java] faster

Yes and no. Java is a simpler language than C++ in that it doesn't have pointers or manual memory management or templates and because it has a big, fully-featured standard library which simplifies things. However, the huge class library takes a lot longer to learn than the smaller C++ standard library. I would say that overall Java is easier to learn, though -- it was designed to be.

[Java] can't be used to run high-cpu calculations (way too slow for that, aka no graphics engines with it, no refrencing hashes too much, etc...)
[C++ is] fast

Again, not necessarily. Java is quite fast and it can be used for stuff like graphics just like C++ can, although you probably wouldn't use it for complex, real-time graphics (though you could just write those parts of your program in C++ and the rest in Java). Minecraft is written in Java and it gets pretty poor performance despite having quite simple graphics (although it does have to simulate a lot of objects; it could probably render much better graphics with fewer objects). Java is only slower because it's effectively two programs running at once -- the JVM, which either interprets, or (more commonly) dynamically recompiles, Java bytecode into native code, and then the actual Java program.

@Lumpkin
C++ has a smaller standard library which doesn't do as much for you, so you'll find yourself implementing things that Java has already got, and you may also find yourself writing more boilerplate code in C++.
Last edited on
"Does that mean you're interested in devising softwares for the stock markets? Then trust me, you'll use a great deal of C++"

I think you're confusing C++ with FORTRAN and COBOL. Shockingly still widespread in the "market" market.
Learn them all, then take over the IT Department:

http://www.newslettercartoons.com//catalog/gifs/3266-w.gif
Meh, there has been many discussions like that before.
I just fix a few misconceptions.

1. Java *does have* pointers. They are called references, but they serve exactly the same purpose C++ pointers are used for 99% of cases for. Java doesn't have pointer arithmetic, but with modern optimizing compilers, pointer arithmetic is of really little value in C++, as indexed array access is exactly as fast (or sometimes faster) as pointer access and you should be using unique_ptr and shared_ptr anyway.

2. The main strength and unique feature of C++ are templates IMHO. Even though they don't feel like a real static type-system (e.g. found in Haskell, F#, Scala or even C# and Java), some performance advantage is still there, and they are pretty powerful and can make high-level abstractions.

3. Java is a perfectly viable choice for computationally intensive server applications, including:
- HFT (see realtime Java system),
- database system engines (so far choice #1 for implementing high performance NoSQL solutions),
- parallel processing analytical engines, for crunching petabytes of data on big clusters (unbeatable: Hadoop, Spark)
- high performance web servers / webapp containers (Netty vs Nginx: tie and there are no things like Tomcat or JBoss in C++ world).

Java is a really poor choice for high performing desktop apps, because of startup overhead and because Sun/Oracle didn't care for desktop experience for too long - Swing being buggy, ugly and poorly integrated as a result.

4. Java-the-language is verbose but you have plenty of cool, more expressive and concise languages integrating seamlessly with the whole platform: Scala and Clojure being my favorite ones (but also Groovy, Kotlin, Ceylon, etc.).

5.
because at least then I know what my program is doing... but that's my preference.
In C++ you don't know what your program is doing any less than in Java. In both you've got a compiler that is between your source code and the machine, and it is allowed to do many crazy things with your code.


Minecraft is written in Java and it gets pretty poor performance despite having quite simple graphics (although it does have to simulate a lot of objects; it could probably render much better graphics with fewer objects).


Simply looking != simple to render. Minecraft has an open-world graphics and the number of objects to render is comparable with what many other AAA games do.
@rapidcoder
I would say RAII is more important than templates in C++. It's automatic, but deterministic memory management. GC is great for simplifying the programming experience, but RAII has a tiny performance overhead and is very nearly as simple and safe as GC. Especially with std::*_ptr.
rapidcoder wrote:
see realtime Java system

Are you referring to RTSJ and its existing implementations? Ahead-of-time compilation, scoped and static memory instead of GC, ahead-of time class initialization, and sane threads.. and a bunch of awkward special rules the programmer must follow to avoid other surprises the spec didn't or couldn't cover (I skimmed through IBM's and Sun's implementation manuals).
Perhaps in another decade this will catch up with last century's C++.
Pages: 123