1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Difference between Java and C++

Discussion in 'Java' started by persysweb, Feb 25, 2018.

  1. persysweb

    persysweb Member

    Joined:
    Aug 1, 2017
    Messages:
    98
    Likes Received:
    15
    Trophy Points:
    8
    Location:
    India
    Home Page:
    Main difference between C++ and Java:

    Both C++ and Java both are object oriented programming language but yet there is difference between the 2 programming language :

    1. Pointers :

    C++ supports pointers which are variables which explicitly stored memory address.
    As every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory.
    Code:
    eg. int main () {
         int  addr = 30;   // actual variable declaration.
         int  *addr1;        // pointer variable
    
         addr1 = &addr;       // store address of addr in pointer variable
    
         cout << "Value of addr variable: ";
         cout << addr << endl;
    
         // print the address stored in addr1 pointer variable
         cout << "Address stored in addr1 variable: ";
        cout << addr1 << endl;
    
         // access the value at the address available in pointer
         cout << "Value of *addr1 variable: ";
         cout << *addr1 << endl;
    
       return 0;
    }
    
    output:
    Value of addr variable: 20
    Address stored in ip variable: 0xbfc601ac
    Value of *addr1 variable: 20

    Java : Java does not support Pointers. But to use pointer in Java we use references but references cannot perform arithmetic pointers in Java and unsafe conversions are also not allowed.

    eg. We access a null Reference through NullPointerException not a NullReferenceException.So clearly programmers were thinking about the Pointers so they wrote this specification.

    2. Destructor :
    Java support automatic garbage collection. It does not support destructors as C++ does.

    C++ support destructors, which is automatically invoked when the object is destroyed.

    3. Default Arguments:
    Java does not support default arguments. There is no scope resolution operator :: in Java. The method definitions must always occur within a class, so there is no need for scope resolution operator.

    C++ supports default arguments. C++ has scope resolution operator :: which is used to to define a method outside a class and to access a global variable within from the scope.

    4. Multiple Inheritance :

    1. Java does not support multiple inheritance because it leads to diamond ring problem. Multiple Inheritance in Java can be achieved by using Interfaces in Java.

    1. C++ does support multiple inheritance. The keyword virtual is used to resolve ambiguities during multiple inheritance if there is any.

    5. Method overloading and operator overloading

    Java has method overloading, but no operator overloading. The String class does use the + and += operators to concatenate strings.

    C++ supports both method overloading and operator overloading.

    6. Compatibility:

    Java is interpreted for the most part and hence platform independent.

    C++ generates object code and the same code may not run on different platforms.
     
    Last edited by a moderator: Feb 28, 2018

Share This Page