Similarities between C++ and Java
People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher role as an SDE (Software Development Engineer), it is advised to choose one among the two and continue preparing for the interview process.
Though both the languages C++ and Java are equally famous for their unique and distinguished features, there are also some considerable similarities among them which are explained as follows where we have discussed the code and also the output clearly explaining the similarity between the two distinguished languages.
- Execution of code similarity starts from the main program only in both c++ and java.
C++ code
#include<iostreaam>;
using namespace std;
int main() {
cout<<"Hello world"<<endl;
return 0;
}
Output
// Note that the execution of C++ programs starts from main function only
Hello duniya
Java code
public class first {
public static void main(String[] args) {
System.out.println(" Hello duniya "); }
}
Output
// Note that the execution of java programs starts from main function only
Hello duniya
- Same code flow similarity in both C++ and java programming languages
C++ code
#include<iostreaam>;
using namespace std;
int main() {
cout<<"Hello world"<<endl;
return 0;
}
Output
// as you can see that we have written in c++ programming language once libraries are fetched the code starts executing and prints the print //statement hello duniya or us.
Hello world
Java code
public class first {
public static void main(String[] args) {
System.out.println(" Hello duniya ");
}
}
Output
// as you can see that we have written in java programming language once libraries are fetched the code starts executing and prints the print //statement hello duniya or us.
Hello duniya
- Ending of the code will be known for us by the semicolon (;)
C++ code
#include<iostreaam>;
using namespace std;
int main() {
cout<<"Hello duniya"<<endl;
return 0;
}
Output
// note that the statements in C++ programs end with a semi colon (;)
Hello duniya
Java code
public class first {
public static void main(String[] args) {
System.out.println(" Hello duniya ");
}
}
Output
// note that the statements in java programs end with a semi colon (;)
Hello duniya
- Variable data type declaration is the same in both c++ and java programming languages, same arithmetic operators to perform addition, subtraction, multiplication, division, etc.
C++ code
#include <iostream>
using namespace std;
int main() {
int a = 8;
int b = 23;
int sum;
sum = a + b;
cout << sum;
return 0;
}
Output
// as you can see we have used the same arithmetic operator to perform addition in c++
// as you can see in the c++ programming language code that we have written variables a, b, and sum are declared with int data type unlike where do not mention it type in the python programming language.
31
Java code
public class GFG {
public static void main(String[] args) {
int a = 8;
int b = 23;
int sum;
sum = a + b;
System.out.println(sum);
}
}
Output
// as you can see we have used the same arithmetic operator to perform addition in java
// as you can see in the java programming language code that we have written variables a, b, and sum are declared with int data type unlike where do not mention it type in the python programming language.
31
- Declaring the conditional statement if-else, elif, etc. are all same in both c++ and java programming languages.
C++ code
#include <iostream>
using namespace std;
int main() {
int a = 8;
int b = 31;
if (a > b)
cout << a;
else
cout << b;
return 0;
}
Output
//as you can see that the conditional statements that we have written to sort out the bigger among the two variables a and are all same in c++ and similar in java as well
31
Java code
public class first_java {
public static void main(String[] args) {
int a = 8;
int b = 31;
if (a > b)
System.out.println(a);
else
System.out.println(b);
}
}
Output
//as you can see that the conditional statements that we have written to sort out the bigger among the two variables a and are all same in java and similar in c++ as well
31