C++ vs. Java: Performance and Control for Modern Development | TechWell

C++ vs. Java: Performance and Control for Modern Development

C++ vs. Java: Performance and Control for Modern Development

Java and C++ are the two most commonly used high-level, general-purpose, object-oriented programming languages. C++ has made a sort of a comeback in recent years, although calling it a comeback would be an exaggeration because it has been one of the top 5 programming languages for years even as other programming languages were introduced and became popular. Most notably, before Java in 1995, C and C++ were the #1 and #2 languages respectively according to the TIOBE index. With the new World Wide Web, the main appeal of Java over C++ was its platform independence, and portability across networks.

Platform independence means that a program compiled on one operating system (OS) can run on another operating system. C/C++ are closer to the operating system kernel, and therefore not platform independent. As an example, a Java program compiled on Windows can run on Linux, but a C++ program may not run especially if the program makes OS kernel system calls.

Java has been the #1 high-level, general-purpose, object-oriented programming language for most of the past two decades only recently being surpassed by C++. TIOBE recognized C++ as the language with the biggest gain in 2022. While slight fluctuations in ranks don’t indicate much, a resurgence of C++ does indicate a shift in the programming preferences and needs.

This article explores what C++ has that Java doesn’t.

Closer to the Operating System

C++ has always been closer to the operating system and the hardware. C is a low-level programming language designed for systems programming. Operating system kernels for Linux and Windows are written in C. As C++ is based on C, it has access to the system-level functions of C. What that means is that a C++ program can make direct, system calls to the operating system. As an example, C++ supports the dynamic memory allocation functions it inherits from C: malloc, realloc, calloc, aligned_alloc, and free. All of which makes C++ a better choice when developing new compilers. Offering the benefits of both a low-level API and a high-level programming language, C++ is more suitable than Java for embedded systems.

Dynamic Memory Management with Smart Pointers

Java has always provided automatic memory allocation and deallocation. Mainly, memory deallocation is an issue if memory associated with objects that are not being used is not promptly reclaimed or deallocated. The JVM’s garbage collector (GC) provides automatic memory deallocation.

Before C++ 11, C++ did not have much support for automatic deallocation of memory associated with unreferenced objects. This leads to possible memory leaks. The delete operator had to be called explicitly to deallocate memory associated with objects created with the new operator. C++ 11 introduced smart pointers for automatically deallocating memory when object use goes out of scope. Because smart pointers for automatic memory management is a relatively new feature, some programmers continue to believe that C++ does not provide any automatic memory management similar to Java’s garbage collector.

Exception Handling Is More Flexible

Both Java and C++ provide a base class for exceptions; std::exception in C++ and java.lang.Throwable in Java. The similarity ends there. While in Java the catch block can only catch a Throwable object, in C++ the catch (...) ellipsis form may be used to catch any type of exception, including primitives and pointers. Specifically, C++ supports catching by value, by reference, and by pointer. Java supports catching an exception by value only; the catch() block must specify an argument type or ExceptionType that is a sub-class of Throwable; a Throwable object.

Some Other Language Features

C++ supports some language features that Java doesn’t. C++ supports the goto statement to unconditionally transfer control to a specific statement. Java provides a slightly more complex custom navigation alternative using labels. C++ supports operator overloading to customize operators to support the user-defined types of the operands. C++ supports function call by reference and call by value while Java only supports call by value. C++ supports structures and unions, which Java doesn’t.

Java Depends on C++

Java was originally developed to overcome some of the complexity of C++, perhaps inspired by C++, and in fact, written in C/C++. The JDK (Java Development Kit) and the JVM are based on C/C++. Parts of the JDK are C or C++ while most of it is written in Java. A good deal of the JVM is written in C or C++ as it has to be a native application running on the OS. Java strives to keep itself up-to-date with new features in C++. JEP (JDK Enhancement Proposal) 347, which was delivered in Java 16, enables C++14 Language Features in JDK C++ source code and HotSpot code.

Java does accomplish its initially set objective of overcoming the complexity of C++ but it is not a replacement for C++. C++ continues to outperform Java, and in fact the only option between the two, for applications that require a closer interaction with the operating system kernel. C++ has other advantages over Java in its smart pointers for dynamic memory management, flexible exception handling, and other sundry features.

Up Next

About the Author

TechWell Insights To Go

(* Required fields)

Get the latest stories delivered to your inbox every month.