×

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

Related Topics

Hierarchical Inheritance

C++ Hierarchical Inheritance Hierarchical inheritance inherits the property of one base class in more than one derived class.   C++ Hierarchical Inheritance Example #include <iostream>   using namespace std;   class Person {       char gender[10];       int age;   public:       void getPerson()       {           cout << "Age: "; cin >> age;           cout << "Gender: "; cin >> gender;       }       void dispPerson()       {           cout << "Age: " << age << endl;           cout << "Gender: " << gender << endl;       }   };   class Employee : public Person {       float salary;   public:       void getEmployee()       {           Person::getPerson();           cout << "Salary: Rs."; cin >> salary;       }       void dispEmployee()       {           Person::dispPerson();           cout << "Salary: Rs." << salary << endl;       }   };   class Student : public Person {       char level[20];   public:       void getStudent()       {           Person::getPerson();           cout << "Class: "; cin >> level;       }       void dispStudent()       {           Person::dispPerson();           cout << "Level: " << level << endl;       }   };   int main()   {       Person per;       Employee emp;       Student stu;       cout << "Student data" << endl;       cout << "Enter data" << endl;       stu.getStudent();       cout << endl << "Displaying data" << endl;       stu.dispPerson();       cout << endl << "Staff Data" << endl;       cout << "Enter data" << endl;       emp.getEmployee();       cout << endl << "Displaying data" << endl;       emp.dispPerson();   } Output: Student data Enter data Age: 10 Gender: f Class: 5 Displaying data Age: 10 Gender: f Employee data Enter data Age:...

1 minute read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.

Leap Year Program in C++

What is a Leap Year? A solar year is the length of time that it takes for Earth to orbit the Sun - approximately 365.25 days. In a calendar year, we...

4 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

Web Development in C++

Before learning above C++ web development, we need to learn about CGI What is CGI? CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of...

4 minutes read.

Find the Size of Array in C/C++ without using sizeof() function

We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of...

3 minutes read.

C++ Keywords

In this article, we will discuss keywords in C++ with their several features and functions. What are Keywords in C++? In C++, a keyword is a reserved word that has a predefined...

4 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Naming Convention in C++

The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves...

5 minutes read.