Java vs C++ brief comparison

Page 1

Author: Imad Mohammad Fahmi Ozone

Date: October 8, 2017

Programming Languages (Focused Discussion) The variety of programming languages in the market makes it hard to tell which is best. Java (the most popular language in the world according to TIOBE) and C++ are a good example of that. While they have core differences, they share many language aspects. The following table shows a comparison between the two languages:

Features Programming Paradigms (OOP, Functional, and Generic)

C++ supports the three paradigms which makes it so powerful. Programmers can simply create a function that doesn’t belong to any class like the following: #include <iostream> std::string Greeting() { return "Hellow World!"; } int main() { std::cout << Greeting(); return 0; } In Java, OOP and Generic are supported, but not global functions. Java has a strict rule that all methods must belong to classes.

OOP (Constructors)

In C++, constructors are very similar to those in Java. In both languages, if you don’t define any class constructor the compiler will use a default, parameter-less, one to create objects. The only difference is that C++ has a copy constructor which is not available for Java due to the fact that all objects are passed by reference (Molloy, 2017).

OOP (Destructors) And Memory Management

C++ allows developers to implement class destructors, which are used to clean up the memory from the object (Molloy, 2017). In C++, destructors are called automatically when the object becomes out of scope. There is a way to control when to destroy an object through the “delete” keyword (En.cppreference.com, 2017). On the other hand, there are no destructors in Java, instead, there is a memory management mechanism called “Garbage Collector”. Garbage Collector (GC) automatically cleans the memory from all un-used variables and reclaims memory for re-use (Oracle.com, 2017).

OOP (Access Modifiers)

Java has four access modifiers (public, private, protected, and package). C++, however, has all but “package”. The default access modifier for class methods and fields in C++ is “private”, and “package” in Java. The “package” modifier is related to the package concept which is like a namespace (docs.oracle.com).

OOP (Multiple Class Inheritance)

C++ supports multiple class inheritance which allows a class to have more than one parent. Sometimes, it causes the famous Diamond problem. Java and even .Net programming languages, like C#, ban it and compensate it by the concept of Interfaces.

Native and Managed

This is almost the biggest difference between the two languages. C++ is a native language,


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.