All Articles

Which should you choose: Java or C++?

Posted 2016-08-22 02:08

4 minutes to read

Java Team at Rozdoum

Many popular questions generate some degree of opinion based on expert experience,

but answers to these questions will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. Read a new article about our expert’s comparison of the programming languages Java and C++.

There are a lot of arguments for and against each one. There’s no way to objectively choose one without having particular criteria.

First of all, a programming language is a tool. You could achieve exciting results using any of them. Basically, developer skills are essential. Although a tool is really important. Sometimes it hints a way to solve a problem. Sometimes you should develop your own using provided features.

Therefore your choice would depend on what your purposes and personal preferences are. So one could assume criteria and then suggest a reasonable choice.

C++ is a multi-paradigm programming language having a minimum overhead possible and a modest standard library. One of the very basic principles of C++ is “You don’t pay for what you don’t use”. Java is a complete platform designed to cover most of the purposes out of the box.

Entry level

Of course, Java and C++ are pretty same when you’re writing a bubble sorting algorithm or a “hello, world” application. Though an entry level for practical development is not the same for these two program languages.

Manual memory management in C++ is known to be powerful but dangerous at the same time. C++ requires more experience to write a safe code. Java code is easier to debug.

As opposed to Java, compiled C++ code (without debug information included) is almost not possible to debug in practice.

By the way, it is harder to migrate from Java to C++ than vice-versa.

Safety

Unlike C++, Java has built-in checks for each statement that can be unsafe, like for an array element access (whether an index is within array bounds or not) or type casting. In C++ unsafe operations (usually related to a manual memory management) are permitted so some bugs are much harder to find. For example writing to a wrong memory area could cause no visible issues for a long time.

Performance

Performance is a very controversial question about Java. First of all, Java has a runtime overhead like every programming language that is not compiled to a native hardware-specific code to be executed by CPU without translation. That statement leads to an obvious conclusion that Java will always be slower than C++.

Although in practice there are a lot of factors affecting a performance. Modern JIT-compilers perform a significant optimization so in some particular cases Java program can even run faster than a program written in C++. Though it’s rather an exception than a rule.

Anyway, a performance difference is considered significant or neglectable depending on actual purposes.

Development

Reflection API is a particular feature that’s worth to be mentioned. Java provides reflection facilities, but C++ doesn’t. Therefore clear and convenient code you can write in Java sometimes is not possible in C++ at all, so you have to find a different approach. To get an example have a look at mocking and serialization libraries.

Compiling a C++ file takes a very long time when compared to a Java.

Java standard library fulfills significantly more needs than the C++’s one (even C++11).

Portability/Compatibility

Basically, Java is portable and C++ is a platform-dependent language. Although, it doesn’t mean that every single Java program can always work on any platform without changes at all. Also, it doesn’t mean that a C++ program should always be rewritten from scratch.

Actually, Java provides a true source code portability (language syntax and semantics are the same for each supported hardware/software platform) and CPU architecture portability (a given Java program produces identical results regardless of the CPU or Java compiler). Operation system portability is limited to capabilities provided by a language core and the standard library.

Even a C++ core is platform-specific. Although, there are libraries (like boost and Qt) that help to make porting quite easy.



Java Team at Rozdoum