C++ Vs Java

C++ Vs Java:

The C++ programming language and Java have many differences and similarities. The following are the key differences between C++ and Java:

Comparison Index C++ Java
Platform-independent C++ is platform-dependent Java is platform-independent
Mainly used for System Programming Application programming. Used in Windows-based, web-based, enterprise, and mobile applications.
Design Goal Extension of C programming language for systems and applications programming Created as an interpreter for printing systems, later extended as a support network computing. Designed to be easy to use and accessible.
Goto Supports the goto statement Doesn’t support the goto statement
Multiple inheritances Supports multiple inheritance Doesn’t support multiple inheritance through class, but can be achieved by using interfaces
Operator Overloading Supports operator overloading Doesn’t support operator overloading
Pointers Supports pointers. You can write a pointer program in C++. Supports pointer internally. However, you can’t write the pointer program in Java. Java has restricted pointer support.
Compiler and Interpreter Uses a compiler only. C++ is platform dependent. Uses both a compiler and an interpreter. Java is interpreted, thus platform-independent.
Call by Value and Call by Reference Supports both call-by-value and call by reference Supports call by value only. No call by reference.
Structure and Union Supports structures and unions Doesn’t support structures and unions
Thread Support Doesn’t have built-in support for threads. Relies on third-party libraries for thread support. Has built-in thread support
Documentation comment Doesn’t support documentation comments Supports documentation comment (/** … */) to create documentation for java source code
Virtual Keyword Supports virtual keywords so that we can decide whether or not to override a function No virtual keyword. We can override all non-static methods by default. Non-static methods are virtual by default.
unsigned right shift >>> Doesn’t support >>> operator Supports unsigned right shift >>> operator that fills zero at the top for negative numbers. Works the same as the like >> operator for positive numbers.
Inheritance Tree Always creates a new inheritance tree Always uses a single inheritance tree because all classes are the child of the Object class. The Object class is the root of the inheritance tree.
Hardware Nearer to hardware Not as interactive with hardware
Object-oriented Object-oriented language. In C language, a single root hierarchy is not possible. Object-oriented language. Everything (except fundamental types) is an object. Single root hierarchy as everything gets derived from java.lang.Object.

Note:

In contrast to C++, Java does not have support for default arguments. Additionally, Java does not use header files as in C++. Instead, the import keyword is used in Java to include different classes and methods.

C++ Program Example

Program: main.cpp

#include <iostream>  
using namespace std;  
int main() {  
   cout << "Hello C++ Programming";  
   return 0;  
}

Output:

Hello C++ Programming

Java Program Example

Program: Sample.java

class Sample{  
    public static void main(String args[]){  
     System.out.println("Hello Java");  
    }  
}

Output:

Hello Java

We hope this article has provided you with insights into the comparison between C++ and Java. For further learning about Java, we encourage you to follow tutorials.freshersnow.com.