Similarities between Java and C++

Last Updated :

Java and C++ are the two most popular programming languages. Although they have many differences, there are quite a lot of similarities between them.

Similarities between Java and C++

Let us look into the similarities between Java and C++.

1. Comments


Comments are same in C++ and Java. Single line comments are done by // and multiple line comments are done by

/*.....*/ . 

Examples have been given below.

2. Main function


In both C++ and Java executation of program is initiated from main() function.

Example –
In both Java and C++ main() function is there but there syntax are quite different.

public class Java {

    //main() function in Java
    public static void main(String[] args) {                   
        // Write your code here
    }
}  

But in C++ it is quite different

//main() function in C++
int main() {                                                   
//Write your code here
} 

3. Classes And Objects


Java is an object-oriented programming language. C++ also supports classes and objects.
(But C++ is not fully object oriented language, a C++ programs can easily be written without concepts of classes and objects.).

Example –
Here we give a simple example of a complex class in C++ and Java.

class Complex{
float re;
float im;
};    

//Note that ‘;’ is not given at the end of classes in Java

4. Control Constructs


Major Control Constructs (for ,while loops if etc.) in C++ and Java are identical.

Example –
In both Java and C++ loops, if-else statements are as follows:

int fact = 1;

for(int i=1;i<=m;i++)
{

//Computes factorial of a given number m 
fact=fact*m;                
} 

5. Primitive Types


Primitive types in Java are almost the same as that of C++.
(Though there are some differences, such as unsigned int data type is present in C++, but Java does not support it. In contrast Java has bigInteger data type that theoretically has no limit but C++ does not support such data types.

Example –
In both C++ and Java primitive types are declared as follows:

int a,b=5;
float c=0.7; 
  • Relational And Arithmetic Operators


    No significant difference.

    Example –

    a +=5;
    b =5*4;
  • Comment
    Next Article
    CPP_Programmer Published 06 Aug, 2020 · 2 min read

    Similar Reads

    • Four Pillars of Object-Oriented Programming (OOPS)

      Get an overview of four pillars of object-oriented programming (OOP) and see their suitable real life examples. What is OOP? Picture a cluttered garage🛠️. Tools are scattered everywhere,…

      4 min read
    • What is Overriding in C++

      Write a program in C++ to depict the concept of overriding or Dynamic polymorphism. Overriding is a type of polymorphism. Polymorphism in OOP languages means to take more…

      2 min read
    • What should we use void main() or int main() ?

      There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

      2 min read
    • Difference between Full Virtualization and Para-virtualization

      Process of abstracting underlying hardware components from software is the primary concept behind virtualization. 1. Full Virtualization In full virtualization, a single host operating system allows the execution…

      2 min read
    • Difference between COM and DCOM

      COM :- COM stands for Component Object Model.The COM is a software architecture that allows application to be built from binary software component. It is introduced in 1993…

      2 min read
    • Difference between Firewall and Proxy Server

      Both firewall and thee proxy are resided between the local system and the internet to provide security against the network threats. 1. Firewall: A firewall is an application…

      2 min read