Similarities between Java and C++
Both are very successful and popular programming languages. Though there are many differences between the both, there are considerable similarities which are given as follows:
- Both C++ and Java supports Object Oriented Programming:
OOPs is a modular approach, which allows the data to be applied within stipulated program area, It also provides the re-usability feature to develop productive logic, which means to give more emphasis on data. It support classes and objects. OOPs features include:
- Inheritance: process by which objects of one class can link and share some common properties of objects from another class.
- Polymorphism: Allows us to perform a single action in different ways. It is the process of using a function for more than one purpose.
- Abstraction: It is the act of representing essential features without including the background details.
- Encapsulation.: Wrapping up of data and functions into a single unit.
- They have similar syntax:
C++ Syntax:
#include& lt; iostream & gt;usingnamespacestd;intmain(){cout& lt;<"Hello World";return0;}chevron_rightfilter_noneJava Syntax:
publicclassfirst {publicstaticvoidmain(String[] args){// prints Hello WorldSystem.out.println(" Hello World ");}}chevron_rightfilter_none - Comments Syntax are identical:
Both the single and multiple line comments are written as //…. and /* …. */ respectively.
C++:
#include <iostream>usingnamespacestd;intmain(){// main() is where program execution beginsinta = 5, b = 10, sum;sum = a + b;/* This will add the values of a and band will display the output stored in sum */cout << sum;return0;}chevron_rightfilter_noneJava:
publicclassGFG {publicstaticvoidmain(String[] args){// main() is where program execution beginsinta =5, b =10, sum;sum = a + b;/* This will add the values of a and band will display the output stored in sum */System.out.println(sum);}}chevron_rightfilter_none - The loops (like while, for etc.) and conditional statements (like if-else, switch etc.) are similar:
C++:
#include <iostream>usingnamespacestd;intmain(){inta = 5, b = 10;if(a > b)cout << a;elsecout << b;return0;}chevron_rightfilter_noneOutput: 10
Java:
publicclassfirstjava {publicstaticvoidmain(String[] args){// to display the greater numberinta =5, b =10;if(a > b)System.out.println(a);elseSystem.out.println(b);}}chevron_rightfilter_noneOutput: 10
- Both have same arithmetic and relational operators.
Arithmetic operators such as +, -, *, / Relational operators such as >, <, =, != (not equal to)
- Execution of both the C++ and Java programs starts from the main function:
It is the entry point of the execution of the program. However, the function declaration is different, but the name is same.C++:
#include& lt; iostream & gt;usingnamespacestd;intmain(){// main() is where program execution beginscout& lt;<"Hello World";return0;}chevron_rightfilter_noneJava:
publicclassGFG {publicstaticvoidmain(String[] args){// main() is where program execution beginsSystem.out.println(" Hello World ");}}chevron_rightfilter_none - They have same primitive data types:
These include datatypes like int, float, char, double etc. with some differences like the Boolean data type is called boolean in Java but it is called bool in C++. - Many of their keywords are same:
Example:break, continue, char, double, new, public, private, return, static etc.
- Both have multi threading support:
Both allow executing multiple threads(sub-processes) simultaneously to achieve multitasking.
Recommended Posts:
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- Similarities and Differences between Ruby and C++
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



