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.
Table of Contents
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
C++ execution process
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.
Features | C++ | Java |
---|---|---|
Abstraction | Yes | Yes |
Encapsulation | Yes | Yes |
Single inheritance | Yes | Yes |
Multiple inheritance | Yes | No |
Static binding | Yes | Yes |
Dynamic binding | Yes | Yes |
Polymorphism | Yes | Yes |
Operator overloading | Yes | No |
Header files | Yes | No |
Pointers | Yes | No |
Global variables | Yes | No |
Template class | Yes | No |
API | No | Yes |
Interference and Packages | No | Yes |
Differences between C++ and Java
Below are the differences between C++ and Java.
Comparison Method | C++ | Java |
---|---|---|
Platform Indepenedence | It is platform dependent. Should be compiled on every platform | It is platform independent. It can be compiled in one platform and executed in another |
Compiler and Interpreter | It is a compiled language | It is a compiled and interpreted language |
Portability | It is not portable | It is portable since the bytecode output can be executed on any system |
Memory management | Memory management is manual | Memory management is system controlled |
Multiple Inheritance | Supports multiple inheritance | Does not support multiple inheritance. But can be achieved through interface |
Overloading | Supports operator overloading | Does not support operator overloading |
Virtual keyword | It uses virtual keyword to override function | It does not use virtual keyword. By default all non-static methods are overridden or virtual |
Pointers | Uses the pointers concept | Does not have pointers concept |
Documentation comments | There is no support for documentation comments | It has built-in support for documentation comments(/** ... */ |
Thread support | C++ does not have in-built thread mechanism and uses third party libraries | Java has in-built thread support using the class "Thread" |
Call by value and Call by reference | Supports both Call by value and Call by Reference | Supports only call by value |
Structure and Union | Supports both structure and union | Does not support Structures and union |
Unsigned right shift >>> operator | Does not support unsigned right shift operator | Supports unsigned right shift operator |
Root hierarchy | There is no root hierarchy | It has a single root hierarchy which is java.lang.Object |
Input mechanism | Uses Cin and Cout for I/O operations | Uses System.in and System.out for I/O operations. |
Goto statement | Supports Goto statement | Does not support Goto statement |
Data and functions scope | Data and functions can reside outside the class | Data and functions should be present within the class |
Object management | Uses new and delete to manage objects | Uses automatic garbage collection to manage objects |
Applications | Used for system programming | Used for application programming like windows-based, web-based, enterprise and mobile applications. |
Type | Both procedural and object-oriented programming language | It is only object-oriented programming language |
Concept | Write once compile anywhere | Write once run anywhere everywhere |
Scope resolution operator (::) | Supports scope resolution operator | Does not support scope resolution operator |
Libraries | Supports low-level functionality | Supports high level functionality |
Destructor | Supports destructors | Does not support destructors |
Runtime error handling | The programmer is responsible for handling runtime errors and exceptions | JVM 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