Java vs C++

Hi all. Continuing the series of articles about our favorite programming language.

This time we will touch upon the eternal question “Java vs C++” on the scale of industrial programming.

Although some things are quite relevant to the choice of language in the early stages of learning, including some things that apply to the choice of language for the Olympics. That’s actually how I chose Java 🙂

The most authoritative source for me was an article by my trainer – Fedor Vladimirovich Menshikov. This article was written when the question “What to study after Pascal” arose 🙂

PS: All copyright laws are observed. This article is published with permission of the author.

You wanted to know which language is better to learn – C++ or Java. I have written
tens of thousands of lines of code in each of them, so I’ll try to tell you
the advantages and disadvantages of each.

Here is a short overview of the differences between the ideologies of these languages.

Let’s look at the applicability of the languages for industrial programming.
creating programs of tens or hundreds of thousands of lines of code.

We will compare the languages by two criteria:

  1. As it is known, the main requirement to a program is correct operation.
    That’s why the main criterion of comparison will be “how much the language disposes to
    to make mistakes.”
  2. The second factor will be “how convenient it is to write in the language”.

Criterion 1. How much the language disposes to make mistakes.
Error 1.1. Referring to something that doesn’t exist.

When you refer to something that doesn’t exist, then the program works
unknown. Say, in 255 cases out of 256 it may work, but in 1
it might fail. And this “unknown something” could also be the data
of another program module.

Error 1.1.1: Referring to a nonexistent array element.

The Java language, like C#, ensures that when indexing an array, you can’t
Refer to a non-existent array element. If a reference to a
a non-existent element is accessed, an exception is thrown.

The C++ language inherited classical arrays from the C language, and in C
array indexing checks are not only absent, but even impossible.
are impossible, because procedures, for example, do not pass the length
of the array in question, only a reference to its beginning.

However, C++ doesn’t force you to use classical arrays. In C++ you can
to write a template class imitating an array with a
ranges. All the difference will be in the description of the variable:
StaticArray a;
instead of
int a[5];
while working with such an “array” can be handled exactly the same way as
work with a classical array.

I wrote such a template for myself, and similar templates are used in large
However, for something small like solving an olympiad
problem this way to make the language safe doesn’t work – to write
StaticArray no one wants to waste such a precious time at the Olympiad.

Mistake 1.1.2: Accessing an object that doesn’t already exist via a pointer.

The Java language, like C#, guarantees that the object won’t be deleted until it can be
The Java language guarantees that an object won’t be deleted unless it can be accessed by a pointer. Therefore, accessing an object that has already been deleted
it is impossible to access an object that has already been deleted.

In C++, the programmer is responsible for deleting objects himself, so nobody
prevent him from creating two pointers to the object, through one pointer to that
one pointer to delete that object, and then use the second pointer to work with the place where the object was.

C++ does not force you to use classical pointers, however. IN C++
you can write a template class which imitates a pointer and checks if there are no
to the object after it has been deleted. Even more valuable is
even more valuable is the class of so called “smart” pointers, which delete the pointed objects themselves
pointers as soon as the last reference disappears. As in the case of
arrays, all the difference in using such a pattern will be in the description of
variable: