Understanding Java’s Programming Mindset Compared to C++

As I have worked as a teaching assistant for C++, I hoped to start learning Java by comparing it with C++. At first glance, Java code does indeed look very similar to C++. But this is just a surface similarity. The primary and most important reminder is:

Despite the similar syntax, C++ and Java are vastly different in design philosophy.

Java Has More Abstraction

When you transition from C to C++, you already feel a loss of control over the machine because C++ does a lot of things behind the scenes, such as the low-level implementation of virtual functions, which are not visible in the code. The feeling is even stronger when moving from C++ to Java; we must trust the compiler more — that is, we need to view problems from Java’s perspective.

Java fundamentally does not grant much low-level access, for example:

  • The sizes of standard data types are fixed and may not match the machine’s “natural” word length;
  • There is no <span>sizeof</span> operator;
  • There are no <span>union</span>, which is commonly used in C/C++ for bitwise operations between different types.

Java Does Not Encourage Premature Optimization

I have always been taught: first make the program run,then make it run fast. However, C++ naturally lends itself to optimizing while writing. For example, many people turn functions into inline without performing performance analysis, but these optimizations come at a cost:a) They expose implementation details in header files;b) The implementation is split across two files, reducing readability and maintainability.

Java does not provide this much control, so there is rarely an “optimization impulse” when writing code. The basic idea is: first write correct and clear code, leaving most optimizations to the compiler. Manual optimization is possible, but Java encourages you todo it later rather thando it early.

Java Has Fewer Idioms

Both C++ and Java are complex languages, but C++ is more complex because it has too many “tricks” and rules to remember, such as:

  • If a destructor, assignment operator, or copy constructor is needed,all three are necessary;
  • Classes with virtual functions must have virtual destructors;
  • Use pre-increment rather than post-increment operators because the former is more efficient;
  • Assignment operators must prevent self-assignment;
  • Use <span>const</span> references instead of passing objects by value;
  • Remember the lifecycle of temporary objects…

Java has far fewer rules to remember: sometimes because it does not support certain operations (like operator overloading), and sometimes because it does it for you (like automatic garbage collection).

Java Is a More “Modern” Language

<span>"If you don't need it, you don't pay the price."</span>

The design intent of C++ is high performance. Every new feature is carefully added to ensure it does not affect the speed of existing features. If you only use the C subset of C++, it can be almost as fast as native C.

The cost of this compatibility is:to want functionality, you must declare it explicitly.

For example, from an object-oriented perspective, it is intuitive for member functions to be default “virtual” (the base class designer does not have to guess which functions will be overridden in derived classes). However, due to function table addressing, virtual functions incur a slight performance penalty, so in C++, functions must be explicitly marked as <span>virtual</span>.

Java does not carry these historical burdens:

  • It does not pursue compatibility with C/C++ (it only borrowed syntax);
  • It does not have to accommodate old compiler/linker technologies (Java requires new technologies to run on the internet);
  • It prioritizes “correctness” over “speed” in design.

Additionally, Java can directly leverage new language technologies — such as more advanced automatic garbage collection and Just-In-Time (JIT) compilation.

Reference Books

Bruce Eckel has written two books, “Thinking in Java” and “Thinking in C++”. Both books introduce the fundamentals of the language step by step with concise code examples.

“Thinking in Java” covers all the core concepts behind Java programming, including: program flow, initialization and cleanup, implementation hiding, class reuse, and polymorphism.

“Thinking in C++” begins with an overview of the essence of object-oriented programming and the software design lifecycle. The book analyzes C++ implementations from basic keywords and programming principles to more advanced topics such as function and operator overloading, virtual inheritance, exception handling, namespaces, and templates.

This article is translated from an article by fu-berlin; click the link at the bottom of the page to read the original.

End

Leave a Comment