Differences between C++ and Java


In this tutorial, we will understand the differences between C++ and Java along with similarities in C++ vs Java with example. Both are object-oriented programming languages.

Differences between C++ and Java

What is C++?

C++ is a programming language that is derived from C. Earlier C++ also had the name “C with classes”. This is because C++ was the first language to introduce classes and objects and also inherits all the properties of C. It also uses the support of SIMULA-67 (the 1st Object-oriented language). We mainly use C++ for application and system development. The founder of C++ is Bjarne Stroustrup.

What is Java?

Java is a pure object-oriented programming language that runs on a virtual machine. It is more secure and portable. It also has an interpreter and we mainly use it for application development. The founder of Java is James Gosling. The initial name of Java was OAK which was a failure. Later, it was acquired by Sun microsystems and now by Oracle corporation.

Similarities of C++ vs Java

Even though there are differences between C++ and Java, there are similarities as well.

Execution: Both are similar in terms of the execution process. The compiler converts the source code into machine code. Additionally, Java has an interpreter which uses the JVM for the execution of the compiled code.

Java execution process

Differences between C++ and Java

C++ execution process

C++ vs Java

Features: Both languages support many features of the OOPs concept. But still, there are few features that one language supports and the other does not. Below is the comparison of the feature support between both languages.

FeaturesC++Java
AbstractionYesYes
EncapsulationYesYes
Single inheritanceYesYes
Multiple inheritanceYesNo
Static bindingYesYes
Dynamic bindingYesYes
PolymorphismYesYes
Operator overloadingYesNo
Header filesYesNo
PointersYesNo
Global variablesYesNo
Template classYesNo
APINoYes
Interference and PackagesNoYes

Differences between C++ and Java

Below are the differences between C++ and Java.

Comparison MethodC++Java
Platform IndepenedenceIt is platform dependent. Should be compiled on every platformIt is platform independent. It can be compiled in one platform and executed in another
Compiler and InterpreterIt is a compiled languageIt is a compiled and interpreted language
PortabilityIt is not portableIt is portable since the bytecode output can be executed on any system
Memory managementMemory management is manualMemory management is system controlled
Multiple InheritanceSupports multiple inheritanceDoes not support multiple inheritance. But can be achieved through interface
OverloadingSupports operator overloadingDoes not support operator overloading
Virtual keywordIt uses virtual keyword to override functionIt does not use virtual keyword. By default all non-static methods are overridden or virtual
PointersUses the pointers conceptDoes not have pointers concept
Documentation commentsThere is no support for documentation commentsIt has built-in support for documentation comments(/** ... */
Thread supportC++ does not have in-built thread mechanism and uses third party librariesJava has in-built thread support using the class "Thread"
Call by value and Call by referenceSupports both Call by value and Call by ReferenceSupports only call by value
Structure and UnionSupports both structure and unionDoes not support Structures and union
Unsigned right shift >>> operatorDoes not support unsigned right shift operatorSupports unsigned right shift operator
Root hierarchyThere is no root hierarchyIt has a single root hierarchy which is java.lang.Object
Input mechanismUses Cin and Cout for I/O operationsUses System.in and System.out for I/O operations.
Goto statementSupports Goto statementDoes not support Goto statement
Data and functions scopeData and functions can reside outside the classData and functions should be present within the class
Object managementUses new and delete to manage objectsUses automatic garbage collection to manage objects
ApplicationsUsed for system programmingUsed for application programming like windows-based, web-based, enterprise and mobile applications.
TypeBoth procedural and object-oriented programming languageIt is only object-oriented programming language
ConceptWrite once compile anywhereWrite once run anywhere everywhere
Scope resolution operator (::)Supports scope resolution operatorDoes not support scope resolution operator
LibrariesSupports low-level functionalitySupports high level functionality
DestructorSupports destructorsDoes not support destructors
Runtime error handlingThe programmer is responsible for handling runtime errors and exceptionsJVM is responsible for runtime errors and exceptions

Example of a simple C++ program

Below is a simple C++ program that prints the text “C++ programming language”.

#include <iostream>

using namespace std;

int main()
{
    cout<<"C++ programming language";

    return 0;
}
C++ programming language

Example of a simple Java program

Below is a simple Java program that prints the text “Java programming language”.

public class HelloJava {

  public static void main(String[] args) {
    System.out.println("Java programming language");

  }

}
Java programming language

 

Reference