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:

  1. 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.


  2. They have similar syntax:

    C++ Syntax:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include& lt; iostream & gt;
    using namespace std;
      
    int main()
    {
        cout& lt;
        <
        "
        Hello World"
        ;
        return 0;
    }

    chevron_right

    
    

    Java Syntax:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    public class first {
        public static void main(String[] args)
        {
            // prints Hello World
            System.out.println(" Hello World ");
        }
    }

    chevron_right

    
    


  3. Comments Syntax are identical:

    Both the single and multiple line comments are written as //…. and /* …. */ respectively.



    C++:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
      
    int main()
    { // main() is where program execution begins
        int a = 5, b = 10, sum;
        sum = a + b;
      
        /* This will add the values of a and b
    and will display the output stored in sum */
      
        cout << sum;
        return 0;
    }

    chevron_right

    
    

    Java:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    public class GFG {
      
        public static void main(String[] args)
        { // main() is where program execution begins
      
            int a = 5, b = 10, sum;
            sum = a + b;
      
            /* This will add the values of a and b
    and will display the output stored in sum */
      
            System.out.println(sum);
        }
    }

    chevron_right

    
    


  4. The loops (like while, for etc.) and conditional statements (like if-else, switch etc.) are similar:

    C++:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
      
    int main()
    {
        int a = 5, b = 10;
        if (a > b)
            cout << a;
      
        else
            cout << b;
        return 0;
    }

    chevron_right

    
    

    Output: 10

    Java:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    public class firstjava {
      
        public static void main(String[] args)
        {
            // to display the greater number
      
            int a = 5, b = 10;
            if (a > b)
                System.out.println(a);
      
            else
                System.out.println(b);
        }
    }

    chevron_right

    
    

    Output: 10


  5. Both have same arithmetic and relational operators.
    Arithmetic operators such as +, -, *, /
    Relational operators such as >, <, =, != (not equal to)
    


  6. 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++:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include& lt; iostream & gt;
    using namespace std;
      
    int main()
    { // main() is where program execution begins
        cout& lt;
        <
        "
        Hello World"
        ;
        return 0;
    }

    chevron_right

    
    

    Java:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    public class GFG {
        public static void main(String[] args)
        {
            // main() is where program execution begins
            System.out.println(" Hello World ");
        }
    }

    chevron_right

    
    


  7. 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++.
  8. Many of their keywords are same:
    Example:

    break, continue, char, double, new, public, private, return, static etc. 


  9. Both have multi threading support:
    Both allow executing multiple threads(sub-processes) simultaneously to achieve multitasking.



Learn Java
Learn C++



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.